Get real-time location through Baidu positioning SDK

Source: Internet
Author: User

There was an article about positioning the base station. At that time, the testing environment was Xiaomi + Mobile, which was relatively smooth.

But in practice, there is a problem-the numbers of China Unicom and China Telecom cannot be located!

The best solution is to use a third-party SDK to locate the problem.

Baidu's positioning SDK is still very good. It can be located through WiFi, GPS, GPRS, and has nothing to do with the operator.

Final effect:

The page contains a button and textview. click the button to start positioning. The result is displayed in textview.

Steps:

Add SDK

Go here to download the latest SDK, which is now 3.3.

Copy the liblocsdk3.so file to the libs/armeabi directory. Copy the locsdk3.3.jar file to the libs directory of the project, select "add jars" from Project Properties> JAVA build path> libraries, select locsdk3.3.jar, and return after confirmation. In this way, you can use Baidu positioning SDK in the program.

Add Service

Add the following in the manifest file:

<service android:name="com.baidu.location.f" android:enabled="true"android:process=":remote" android:permission="android.permission.BAIDU_LOCATION_SERVICE">    <intent-filter>          <action android:name="com.baidu.location.service_v3.3"></action>    </intent-filter></service>

Declare Permissions

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission><uses-permission android:name="android.permission.READ_LOGS"></uses-permission>

Main activity code

Package COM. example. locationtest; import android. OS. bundle; import android. OS. vibrator; import COM. baidu. location. bdlocation; import COM. baidu. location. bdlocationlistener; import COM. baidu. location. locationclient; import COM. baidu. location. locationclientoption; import COM. baidu. location. bdnotifylistener; import android. app. activity; import android. app. service; import android. util. log; import android. view. menu; Import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private locationclient mlocationclient; Public bdlocationlistener mylistener; private button mbutton; private textview MTV; private vibrator mvibrator01 = NULL; private Boolean misstart; @ overrideprotected void oncreate (bundle savedinst Ancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mbutton = (button) findviewbyid (R. id. button1); MTV = (textview) findviewbyid (R. id. textview1); misstart = false; mlocationclient = new locationclient (this. getapplicationcontext (); mylistener = new mylocationlistener (); mlocationclient. registerlocationlistener (mylistener); locationclientoption locationoption = new loca Tionclientoption (); locationoption. setopengps (true); locationoption. setcoortype ("bd09ll"); locationoption. setpriority (locationclientoption. networkfirst); locationoption. setaddrtype ("all"); locationoption. setprodname ("baidulocation"); locationoption. setscanspan (5000); // set the interval for initiating a request to 5000mslocationoption. disablecache (true); // disable enabling the locationoption for cache locating. setpoinumber (5); // The maximum number of POI returned locationoption. setpoidist Ance (1000); // poi query distance from locationoption. setpoiextrainfo (true); // whether the poi phone number, address, and other details are required. mlocationclient. setlocoption (locationoption); log. I ("Baidu", "baidumapmylocationactivity enables positioning"); mlocationclient. start (); mbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {// todo auto-generated method stubif (! Misstart) {mlocationclient. start (); mbutton. settext ("stop"); misstart = true;} else {mlocationclient. stop (); misstart = false; mbutton. settext ("START") ;}}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_main, menu); Return true;} public class mylocationlistener implements bdlocationlistener {@ overridepublic void onreceivelocation (bdlocation location) {If (location = NULL) return; stringbuffer sb = new stringbuffer (256); sb. append ("time:"); sb. append (location. gettime (); sb. append ("\ nerror Code:"); sb. append (location. getloctype (); sb. append ("\ nlatitude:"); sb. append (location. getlatitude (); sb. append ("\ nlontitude:"); sb. append (location. getlongpolling (); sb. append ("\ nradius:"); sb. append (location. getradius (); If (location. getloctype () = bdlocation. typegpslocation) {sb. append ("\ nspeed:"); sb. append (location. getspeed (); sb. append ("\ nsatellite:"); sb. append (location. getsatellitenumber ();} else if (location. getloctype () = bdlocation. typenetworklocation) {sb. append ("\ naddr:"); sb. append (location. getaddrstr ();} MTV. settext (SB); logmsg (sb. tostring ();} public void onreceivepoi (bdlocation poilocation) {If (poilocation = NULL) {return;} stringbuffer sb = new stringbuffer (256); sb. append ("Poi time:"); sb. append (poilocation. gettime (); sb. append ("\ nerror Code:"); sb. append (poilocation. getloctype (); sb. append ("\ nlatitude:"); sb. append (poilocation. getlatitude (); sb. append ("\ nlontitude:"); sb. append (poilocation. getlongpolling (); sb. append ("\ nradius:"); sb. append (poilocation. getradius (); If (poilocation. getloctype () = bdlocation. typenetworklocation) {sb. append ("\ naddr:"); sb. append (poilocation. getaddrstr ();} If (poilocation. haspoi () {sb. append ("\ npoi:"); sb. append (poilocation. getpoi ();} else {sb. append ("nopoi information");} MTV. settext (SB); logmsg (sb. tostring () ;}} private void logmsg (string s) {system. out. println (s) ;}@ overridepublic void ondestroy () {mlocationclient. stop (); super. ondestroy ();}}

Relevant documents can refer to Baidu documents-http://developer.baidu.com/map/geosdk-android-classv3.3.htm

Download demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.