Chapter 6 Section 3 Baidu Map Positioning


Introduction

This set of positioning SDK is free to the public, and there is no limit on the number of times the interface can be used. Before using, you need to apply for the key (AK) before you can use it

Positioning principle

To use Baidu Android positioning SDK, you must register for GPS and network usage permissions. The positioning SDK uses GPS, base station, and Wi-Fi signals for positioning. When an application initiates a positioning request to the positioning SDK, the positioning SDK will generate the corresponding according to the actual situation of the application's positioning factors (GPS, base station, Wi-Fi signal) (such as whether GPS is turned on, whether it is connected to the network, whether there is a signal, etc.) Positioning is based on positioning.

Users can set the positioning basis to meet their own needs:
if the user sets GPS priority, GPS will be used for positioning first. If GPS positioning is not turned on or there is no available location information, and the network connection is normal, the positioning SDK will return to the network positioning (ie Wi-Fi). Fi and base station). In order to obtain more accurate network positioning results, please turn on the Wi-Fi switch of the mobile phone.

Environment configuration

  1. Import the library file (liblocSDK7.so).
  2. Add positioning service (AndroidManifest.xml).
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote">
<intent-filter>
<action android:name="com.baidu.location.service_v5.0.0"/>
</intent-filter>
</service>
  1. Add permissions (AndroidManifest.xml)
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- 用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

❖Steps to locate the SDK

  1. Create a location service client (LocationClient).
  2. Set the location service client (LocationClientOption).
  3. Implement the listener interface (BDAbstractLocationListener). Location monitoring
  4. Start location service (LocationClient.start())

Class LocationClient

-The client class of the location service.
The LocationClient class must be declared in the main thread and requires Context type parameters. Context needs to be a valid context for the whole process, it is recommended to use getApplicationConext to obtain a valid context for the whole process.

public LocationClient mLocationClient = null;
	public void onCreate() {
    
    
	mLocationClient= new LocationClient(getApplicationContext());
}

Insert picture description here

Class LocationClientOption

-Configure the positioning SDK parameter class.
Configure various configuration parameters of the positioning SDK, such as positioning mode, positioning time interval, coordinate system type, etc.

The main parameters are: Positioning mode, return coordinate type, whether to turn on GPS, etc.
There are three types of positioning modes:

  1. High-precision positioning mode: Use network positioning and GPS positioning at the same time, and return the highest-precision positioning results first.
  2. Low-power positioning mode: do not use GPS, only use network positioning (WiFi and base station).
  3. Only use device positioning mode: Do not use network positioning, only use GPS for positioning. However, positioning in indoor environments is not supported in this mode.

Insert picture description here

Configure the positioning SDK parameter class

❖ Coordinate type:
➢ Longitude and Latitude Coordinate System of National Bureau of Survey: "gcj02"
➢ Baidu Mercator Coordinate System: "bd09"
➢ Baidu Longitude and Latitude Coordinate System: "bd09ll"
#####GPS:wgs84
❖ Positioning mode:
➢ High-precision positioning mode: High_Accuracy
➢ Low-power positioning mode: Battery_Saving
➢ Device positioning mode only: Device_Sensors

// 设置定位的相关配置
LocationClientOption option= new LocationClientOption();
	option.setOpenGps(true); // 打开GPS
	option.setCoorType("bd09ll"); // 坐标类型
	option.setScanSpan(1000); // 扫描间隔
// 定位模式
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
// 通过配置参数对定位客户端进行设置
mLocationClient.setLocOption(option); 

Implement location request callback interface (BDLocationListener)

// 定位服务客户端设置回调接口
locationClient.registerLocationListener(locationListener);
class MyLocationListener extends BDAbstractLocationListener{
    
    
// 异步返回的定位结果
	@Override
	public void onReceiveLocation(BDLocation location) {
    
    
	// 处理定位信息
	}
} 

❖Class BDLocation Callback Baidu coordinate class.

Insert picture description here

Insert picture description here

At this point, a basic map positioning function can be completed. If you want to display on the map, just like Baidu, a point appears to indicate the location of the map, you need to use the following two categories:
➢MyLocationConfiguration (configuration location layer display mode)
➢MylocationData (location data).

❖Class MyLocationConfiguration

  • Configure the display mode of the positioning layer.
    Set the location layer information through the BaiduMap.setMyLocationConfigeration() method.
public MyLocationConfiguration(MyLocationConfiguration.LocationMode mode,
		boolean enableDirection,
		BitmapDescriptor customMarker)
参数:
mode - 定位图层显示方式, 默认为 LocationMode.NORMAL 普通态
enableDirection - 是否允许显示方向信息
customMarker - 设置用户自定义定位图标,可以为 null
❖Display mode of positioning layer:

➢ MyLocationConfiguration.LocationMode.COMPASS compass state, display the positioning direction circle, keep the positioning icon in the center of the map
➢ MyLocationConfiguration.LocationMode.FOLLOWING follow state, keep the positioning icon in the center of the map
➢ MyLocationConfiguration.LocationMode.NORMAL normal state: the map is not correct when updating the positioning data Do any operation

Class MyLocationData

  • Location data class, MyLocationData contains an internal class Builder for building objects
// 构造定位数据
MyLocationData locData = new MyLocationData.Builder()
	.accuracy(radius) // 精度
	.direction(direction)// 方向
	.latitude(latitude) // 纬度
	.longitude(longitude)// 经度
	.build(); // 构建生成定位数据对象
// 通过BaiduMap.setMyLocationData()设置定位数据
bdMap.setMyLocationData(locData); 

The latitude and longitude of the positioning can be obtained through the onReceiveLocation() method of positioning, and then the positioned point can be moved to the center of the map through the animateMapStatus() method

LatLng ll = new LatLng(mCurrentLantitude,mCurrentLongitude);
MapStatusUpdate u= MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);

Turn on positioning

❖Open the location service through the start() function of the location service client, and close the location service through the stop() function. ❖Because positioning is also more power-consuming, we usually turn on positioning in onStart of Activity and turn off positioning in onStop, so that when the application is minimized, GPS request positioning will not be performed all the time.

protected void onStart() {
    
    
	mBaiduMap.setMyLocationEnabled(true); // 开启图层定位
		if (!mLocationClient.isStarted()) {
    
    
		mLocationClient.start();
}
super.onStart();
}
protected void onStop() {
    
    
	mBaiduMap.setMyLocationEnabled(false); // 关闭图层定位
	mLocationClient.stop();
	super.onStop();
}

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/109799454