Baidu map Android positioning SDK is a set of easy-to-use positioning service interfaces for Android mobile applications, focusing on providing the best comprehensive positioning services for developers. By using Baidu positioning SDK, developers can easily realize intelligent, accurate and efficient positioning functions for applications.
Open Baidu map sdk development and enter the application name. SHA1 needs to be entered here. For the specific search process of SHA1, please refer to the official documents
After the creation is successful, download the official jar package and add all the extracted files to the lib directory in the project file used when applying for AK. Then configure the sourceSets tag in the android block in the build.gradle file in the app directory, as follows:
sourceSets { main { jniLibs.srcDir 'libs' } }
At this point, you can add some directories to the project view. Now you need to add the jar file to the project view. Add the following code to the dependencies block of build.gradle in the app directory, right-click the target jar file and select Add as library to add it to the project view.
implementation files('libs\\BaiduLBS_Android.jar')
Then open the AndroidManifest.xml file and add the following code to < Application > to configure the development key AK
And add permissions according to the official documents
Display map
We input it directly in the code of the xml file
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="411dp" android:layout_height="662dp" android:clickable="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="MissingClass" />
Then we need to initialize the map and create an activity
package com.example.zty_map; import androidx.appcompat.app.AppCompatActivity; import android.app.Application; import android.os.Bundle; import com.baidu.mapapi.CoordType; import com.baidu.mapapi.SDKInitializer; public class MAPZTY1 extends Application { @Override public void onCreate() { super.onCreate(); //in use SDK Initialization before each component context Information, incoming ApplicationContext SDKInitializer.initialize(this); //Since 4.3.0 Baidu map SDK All interfaces support Baidu coordinates and National Survey Bureau coordinates. Use this method to set the coordinate type you use. //include BD09LL and GCJ02 Two coordinates. The default is BD09LL Coordinates. SDKInitializer.setCoordType(CoordType.BD09LL); } }
And declare the application in the AndroidManifest
Then you can display the map by creating an instance of the map, MyMap
mMapView = findViewById(R.id.bmapView); mBaiduMap=mMapView.getMap(); mBaiduMap.setMyLocationEnabled(true);
Display positioning
First, we need to add permissions
<!-- This permission is used for network location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- This permission is used to access GPS location --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Then declare the located service component in the application of AndroidManifest
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/>
Then add the built-in class in mainactivity
class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation location) { //mapView After destruction, it is not in the location where the new receipt is processed if (location == null || mMapView == null) { return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // Set the direction information obtained by the developer here, clockwise 0-360 .direction(location.getDirection()).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); } }
Then open location listening in oncreate
option.setOpenGps(true);
Set positioning parameters:
LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // open gps option.setCoorType("bd09ll"); // Set coordinate type option.setScanSpan(1000);
Finally, turn on the positioning function to display the positioning
mLocationClient.start();
mLocationClient.requestLocation();
Finally, run to check the location. Because it is a virtual machine, you need to import a kml file to display the location
GitHub address: https://github.com/Nanne1ess/ZTY-MAP.git