Objective
Recently the company to the Baidu Map integration project, so I studied the day Baidu Map SDK, the current version: Android SDK v3.0.0. Although Baidu Map online related code more, most likely are older versions, and Baidu Map API changes are also relatively large, so to share with you the implementation of the process and considerations
First, the Application key
Difficult point: Get the SHA1 value of the Android signing certificate
Methods one, using Keytool
Go to the command prompt
Get the SHA1 value of debug:
Navigate to the. Android folder, enter keytool -list -v -keystore debug.keystore
, get three fingerprint certificates, select the SHA1 type of certificate (key password is Android)
Gets the SHA1 value of the release:
Navigate to the folder Xxx.keystore the signature file, enter keytool -list -v -keystore xxx.keystore
, get three fingerprint certificates, and select the SHA1 type of certificate (the key password was previously customized)
Method II, Eclipse, view directly in ADT
Click on the toolbarwindows -> preferance -> android -> build
Second, the Guide warehousing documents
It's best to copy the. so file and jar bag directly inside the demo, and don't forget locsdk.so and Locsdk.jar .
Configure the use of so in Build.gradle
Sourcesets {
main {
jnilibs.srcdirs = [' Libs ']
}
}
Iii. Configuring Androidmanifest.xml Files
(1) declaring permission to use
<!--This permission is used for network positioning--> <uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION" ></ Uses-permission> <!--This permission is used to access GPS location--> <uses-permission android:name= "android.permission.ACCESS_FINE_ LOCATION "></uses-permission> <!--to access WiFi network information, WiFi information will be used for network positioning--> <uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission> <!--obtain carrier information to support the interface to provide carrier information--> < Uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission> <!-- This permission is used to obtain WiFi access and WiFi information is used for network positioning--> <uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" ></uses-permission> <!--used to read the current status of the phone--> <uses-permission android:name= "android.permission.READ_ Phone_state "></uses-permission> <!--write to the extended storage, write data to the expansion card to write offline location data--> <uses-permission Android:name = "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission> <!--access network, network positioning needs to surf the Internet--> <Uses-permission android:name= "Android.permission.INTERNET"/> <!--SD card read access, user writes offline location data--> < Uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission>
(2) Declare service components in the Application tab, each app has its own separate location service
<service android:name= "COM.BAIDU.LOCATION.F" android:enabled= "true" android:process= ": Remote"/>
(3) Set Acesskey
Key: Developer Application Key<meta-data android:name= "Com.baidu.lbsapi.API_KEY" android:value= "key"/>
Iv. initializing the SDK in application
public class MyApp extends application {
@Override public
void OnCreate () {
super.oncreate ();
Sdkinitializer.initialize (this);
}
Note Configuring application in the Androidmainfest.xml file
Five, positioning function encapsulation
Import Android.content.Context;
Import Com.baidu.location.BDLocationListener;
Import com.baidu.location.LocationClient;
Import com.baidu.location.LocationClientOption;
/** * Baidu Positioning Package/public class Baidulocationutil {private Locationclient locationclient;
private static Baidulocationutil Locationutil; public static Baidulocationutil getinstance () {if (Locationutil = null) {synchronized (Baidulocationutil.class)
{if (locationutil==null) {locationutil = new baidulocationutil ();
}} return locationutil; } public void GetLocation (context context, Bdlocationlistener listener) {locationclient = new Locationclient (cont
EXT);
Set positioning criteria locationclientoption option = new Locationclientoption (); Option.setopengps (TRUE); Whether to open the GPS Option.setcoortype ("Bd09ll");
Sets the coordinate type of the return value. Option.setpriority (Locationclientoption.networkfirst); Set positioning priority Option.setscanspan (5000); Sets the interval of 5000ms Opti to initiate a location requestOn.setisneedaddress (TRUE);//setting requires address information, default does not require locationclient.setlocoption (option);
Locationclient.registerlocationlistener (listener);
Locationclient.start ();
public void Unregister (Bdlocationlistener listener) {locationclient.stop ();
Locationclient.unregisterlocationlistener (listener); }
}
Use
(1), Declaration positioning result callback
Locates the result callback, overrides the Onreceivelocation method
private Bdlocationlistener Mlistener = new Bdlocationlistener () {
@Override Public
void Onreceivelocation (bdlocation location) {
String address=location.getaddrstr ();
Tvlocation.settext (address);
}
;
(2), in onCreate()
the binding service
Baidulocationutil.getinstance (). GetLocation (Getapplicationcontext (), Mlistener);
(3), in onDestroy()
the cancellation of services
Baidulocationutil.getinstance (). Unregister (Mlistener);
Summarize
This is the full content of this article, I hope you can help the Android developers, if you have questions you can also message exchange.