The preparatory work

Log in to the Baidu API console

API console address is: lbsyun.baidu.com/apiconsole/…

Create an

After entering the API console, click Create Application and start filling in the relevant information.

To obtain SHA1, enter the Terminal tool in Android Studio. Keytool -list -v -keystore ~/. Android /debug.keystore -alias androiddebugkey (note the directory selection, development version, and release version) Password: The original password is Android

How to obtain the package name: The package name should be applicaionId to prevent AK authentication failure caused by the difference between the applicationId in build.gradle and the package name in AndroidMainfest.xml.

Confirm all the preceding information and click Submit. The system automatically generates AK.

Implement key code

Step 1 Configure the androidmanifest.xml file

1. Add the following code to configure the development key (AK) :

<application> <meta-data android:name=" com.baidu.lbsapi.api_key "Android :value=" developer key" /> </application>Copy the code

2. Add a permission statement externally

3. In the Application TAB, declare the located Service component

<service android:name="com.baidu.location.f"
    android:enabled="true"
    android:process=":remote"/>
Copy the code

The second step is to add the map container to the corresponding layout file

MapView is a subclass of View for placing a map in an Android View. MapView is used in the same way as any other View available on Android.

Step 3 Map initialization

In every function of the SDK components require tuning before using “SDKInitializer. The initialize (getApplicationContext ())”, so to initialize the SDK reference when applied to create the Context of global variables. Create a new custom Application and initialize the SDK in its onCreate method.

Declare the Application in the androidmanifest.xml file

Constructing map data

We get location data and pass it to MapView by inheriting the abstract class BDAbstractListener and overriding its onReceieveLocation method.

// Location initialization

mLocationClient = new LocationClient(getActivity());

// Note that since this Activity is a Fragment, the Context can be obtained by using getActivity() instead of this

// Register the listener function

mLocationClient.registerLocationListener(myListener);

Step 4 Configure locating SDK parameters

LocationClientOption = new LocationClientOption(); option.setIsNeedLocationDescribe(true); / / need to get the position information of the current point, here must be a true mLocationClient. SetLocOption (option). //mLocationClient is the initialized LocationClient object // The configured LocationClientOption object, For more configuration of the LocationClientOption class passed to the LocationClient object via the setLocOption method, see the Class Reference for a detailed description of the LocationClientOption classCopy the code

Step 5 Manage the MapView life cycle in the corresponding Activity

Override onResume(), onPause(), and onDestroy()

Step 6, realize BDAbstractLocationListener interface

The core code is as follows:

public class MyLocationListener extends BDAbstractLocationListener{ @Override public void onReceiveLocation(BDLocation Location) {/ / the BDLocation here for the positioning result information, through its various positioning the get method can obtain all relevant results String locationDescribe = location. GetLocationDescribe (); // Get location description}}Copy the code

The seventh step is to obtain location information

// Enable the map location layer mlocationClient.start ();Copy the code

Implementation effect

// The vm does not have the location function, so its location is not the real location

Baidu official offers a detailed programming specification, the reference site for lbsyun.baidu.com/index.php?t…

Attached project source code address: github.com/learnway299…