Getting local location information in real time using BaiduMap SDK in Android Studio

  • 2021-06-28 09:46:34
  • OfStack

Configure the BaiduMap environment

1. Create a new APP package name and APP name in Baidu API, which should be consistent with the package name and APP name in Baidu Android Studio.

2. An SHA1 digital signature also needs to be filled in in the map of Baidu:

a, enter keytool - list - v - keystore debug.keystore, you will get three fingerprint certificates, select SHA1 type certificate (key password is android), this obtained SHA1 value is the same as the value obtained in ecplise, is used as debug.

b, enter keytool - list - v - keystore XXX.keystore, (where xxx.keystore is the keystore when you generated the app signature), keystore password: Generate the password you entered during xxx.keystore when you signed APP, and the SHA1 you obtained at this time is the release version.

(Essentially for AppKey, there is a problem with values that are not read by AppKey)

3. As needed (http://lbsyun.baidu.com/sdk/download?selected=location) Download the SDK package that I need, because I only download the package of location function here because it only implements location.

4. Unzip the downloaded package. To make app more compatible, I copied all files and folders under libs to libs in app.

5. Configure the Manifest file by first adding the AppKey you obtained to Application:


<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value=" Here's your application AppKey" /> 

Next, you need to register an service specifically for BaiDuMap (you don't need to do it yourself):


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

Finally, configure permissions to locate a list of required permissions (which are also available on official documents):


<!--  This privilege 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"/>
<!--  For access wifi Network information, wifi Information will be used for network location -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!--  Get operator information to support providing operator information related interfaces -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--  This permission is used to obtain wifi Access rights for, wifi Information will be used to locate the network -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!--  Used to read the current status of the phone -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!--  Write Extended Storage, write data to Extension Card, write offline location data -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--  Access network, network positioning needs to go online -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD Card Read Rights, User Writes Offline Location Data -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 

This environment configuration is complete.

Implement Functional Code

1. Write an TextView at layout to store information about your location:


<TextView
android:id="@+id/mytext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/> 

2. A positioning callback interface BDLocationListener and class ES106 EN are needed in positioning:

(1) An onReceiveLocation (BDLocation location) localization callback function in BDLocationListener returned an BDLocation localization result;

(2) The LocationClient class instantiates and implements a location monitoring function (registerLocationListener (BDLocationListener listener));

Define TextView controls and other method classes:


private TextView textView = null;
private Button button = null;
private LocationClient locationClient = null;
public BDLocationListener myListener = new MyBdlocationListener(); 

Implement BDLocationListener (one of the judgements inside can think about and add on your own or try out what location information you need, here's just the current address):


public class MyBdlocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
textView.setText(location.getAddrStr()); } } 

Get all the controls and instances, respectively:


textView = (TextView) findViewById(R.id.mytext);
button = (Button) findViewById(R.id.mybtn);
locationClient = new LocationClient(getApplicationContext());
locationClient.registerLocationListener(myListener);
initLocation();// Initialization LocationgClient
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (locationClient.isStarted()){
locationClient.stop();
}
locationClient.start();
}
}); 

Initialize LocationgClient:


private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
);// Optional, default high precision, set positioning mode, high precision, low power consumption, device only 
option.setCoorType("bd09ll");// Optional, default gcj02 , set the coordinate system of the returned positioning result 
int span=1000;
option.setScanSpan(span);// Optional, default 0 That is, locate only 1 Secondly, set the interval at which location requests are initiated to be greater than or equal to 1000ms Is Effective 
option.setIsNeedAddress(true);// Optionally, set whether address information is required, not by default 
option.setOpenGps(true);// Optional, default false, Set whether to use gps
option.setLocationNotify(true);// Optional, default false , set whether gps When valid, follow 1S1 Secondary frequency output GPS Result 
option.setIsNeedLocationDescribe(true);// Optional, default false To set whether the result of location semantics is required, you can BDLocation.getLocationDescribe Here, the result is similar to "near Tian'anmen, Beijing" 
option.setIsNeedLocationPoiList(true);// Optional, default false To set whether POI As a result, you can BDLocation.getPoiList Get it here 
option.setIgnoreKillProcess(false);// Optional, default true , Positioning SDK Inside is 1 individual SERVICE And put it in a stand-alone process to set whether stop When killing this process, do not kill by default 
option.SetIgnoreCacheException(false);// Optional, default false , set whether to collect CRASH Information, default collection 
option.setEnableSimulateGps(false);// Optional, default false To set whether filtering is required gps Simulation results, default requirements 
locationClient.setLocOption(option);
} 

Now that all the steps are complete, you can try running 1!

The knowledge of using BaiduMap SDK to get local location information in Android Studio is introduced here, and I hope it will be helpful for you!


Related articles: