Location technology is essential for today's mobile devices, and many apps use the location feature.
There are usually two kinds of positioning methods: GPS positioning, network positioning.
The Android system provides API support for both of these targeting methods, but because Google's Web services are inaccessible in China, the Android native location API is rarely used in China, but rather uses the SDK of some domestic third-party companies (such as Baidu, gold). This study is Baidu's lbs (need to apply for API Key in advance, download the appropriate package).
Lbsyun.baidu.com
1, add the Jnilibs directory under the Gradle file (Module:app) android tab, here is dedicated to store so file. (The Jnilibs directory that is displayed under options for Android and the Select project option below Libs is actually a)
sourcesets{ main{ jnilibs.srcdirs=[' libs '} }
Then sync update gradle will be more out of the Jnilibs folder, and then copy the downloaded compressed package file into the current folder, in sync can use the LBS service.
2, in the Androidmanifest.xml Declaration of authority, add API Key in the application tag, and then register a service in the Baidu SDK
<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission Android:name= "Android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/> <uses-permission android:name=" android.permission.ACCESS_ Wifi_state "/> <uses-permission android:name=" Android.permission.CHANGE_WIFI_STATE "/> < Uses-permission android:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permission android:name=" Android.permission.INTERNET "/> <uses-permission android:name=" android.permission.MOUNT_UNMOUNT_ Filesystems "/> <uses-permission android:name=" Android.permission.WAKE_LOCK "/>
<!--Baidu Map-- <service android:name= "COM.BAIDU.LOCATION.F" android:enabled = "true" android:process= ": Remote" > </service> <meta-data android:name= " Com.baidu.lbsapi.API_KEY " android:value=" Uirhfcxvurf8itgcfexzhqpfmutb72tq "/>
You can use the LBS service after you have completed these operations.
3, the basis of lbs use method
① first needs an locationclient instance, Locationclient's build function receives a context parameter, where the Getapplicationcontext () method is called to get a global context parameter and pass in.
public class mainactivity extends appcompatactivity { public locationclient mlocationclient; @Override protected void OnCreate (Bundle savedinstancestate) { super .oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); mlocationclient =new Locationclient (Getapplicationcontext ()); }}
② then calls Locationclient's Registerlocationlistener () method to register a locator listener, which, when the location information is obtained, will be recalled.
Where the locator listener can customize the invocation of different information (longitude, latitude, positioning time, type of position light)
Public classMainactivityextendsappcompatactivity { Publiclocationclient mlocationclient; Public Bdlocationlistener MyListener = new Mylocationlistener (); Public classMylocationlistenerImplementsbdlocationlistener{@Override Public voidonreceivelocation (Bdlocation bdlocation) {}} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mlocationclient=Newlocationclient (Getapplicationcontext ()); Mlocationclient.registerlocationlistener (mylistener); }}
Customize in the listener (you can get the latitude and longitude and positioning information below)
Public classMylocationlistenerImplementsbdlocationlistener{@Override Public voidonreceivelocation (bdlocation bdlocation) {StringBuilder sb=NewStringBuilder (); Sb.append ("Latitude:"). Append (Bdlocation.getlatitude ()). Append ("\ n"); Sb.append ("Longitude:"). Append (Bdlocation.getlongitude ()). Append ("\ n"); Sb.append ("Positioning method:"); if(Bdlocation.getloctype () = =bdlocation.typegpslocation) {Sb.append ("GPS"); }Else if(Bdlocation.getloctype () = =bdlocation.typenetworklocation) {Sb.append (Network); } } }
③ finally calls the Locationclient start () method to start positioning.
Mlocationclient.start ();
④ add TextView in the layout to display the positioning information. The entire mainactivity code is as follows:
Public classMainactivityextendsappcompatactivity { Publiclocationclient mlocationclient; PublicBdlocationlistener MyListener =NewMylocationlistener (); PrivateTextView Positontext; Public classMylocationlistenerImplementsbdlocationlistener{@Override Public voidonreceivelocation (bdlocation bdlocation) {StringBuilder sb=NewStringBuilder (); Sb.append ("Latitude:"). Append (Bdlocation.getlatitude ()). Append ("\ n"); Sb.append ("Longitude:"). Append (Bdlocation.getlongitude ()). Append ("\ n"); Sb.append ("Positioning method:"); if(Bdlocation.getloctype () = =bdlocation.typegpslocation) {Sb.append ("GPS"); }Else if(Bdlocation.getloctype () = =bdlocation.typenetworklocation) {Sb.append (Network); } positontext.settext (SB); }} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Positontext=(TextView) Findviewbyid (R.id.position_text); Mlocationclient=Newlocationclient (Getapplicationcontext ()); Mlocationclient.registerlocationlistener (MyListener); Mlocationclient.start (); }}
Use the simulator to run, complete the simple positioning function
4. Setting the properties of Locationclient
Before the start () method, create a new instance object of Locationclientoption, set some properties through this object, and finally call these settings through the Locationclient setlocoption () method.
Mlocationclient.registerlocationlistener (MyListener); locationclientoption Option =new locationclientoption (); Option.setlocationmode (LocationClientOption.LocationMode.Hight_Accuracy); //Optional, default high precision, set positioning mode, high accuracy, low power, device onlyOption.setcoortype ("Bd09ll"); //Optional, default GCJ02, set the returned positioning result coordinate system intspan = 1000; Option.setscanspan (span); //Optional, default 0, that is, to locate only once, set the interval for initiating a location request is greater than or equal to 1000ms is validoption.setisneedaddress (true); //Optional, set whether address information is required and is not required by default. When set to true, the specific area street information can be obtained through Getcountry (), Getprovice (), getcity () and other methods in listener.Option.setopengps (true); //Optional, default false to set whether to use GPS mlocationclient.setlocoption (option); Mlocationclient.start ();
My Android Development path--Baidu Map Open Source Tool get location information