Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

百度地圖開發學習(二):地圖定位功能全流程解析

編輯:關於 Android 開發

百度地圖開發學習(二):地圖定位功能全流程解析

定位是地圖開發中不可迴避的重要環節,而我也在學習的道路上不斷摸索。以下整理一下自己對百度定位 SDK 的理解與實踐。

一、配置

1. 百度定位有自己的 jar 包和 so 檔,記得把它們放到對應的目錄並加載進工程。具體加載方法在上一篇已講過,這裡不再贅述。

2. 導入庫檔之後,還需要宣告 libs 源檔。

如果把 jar 包和 so 檔全部放進 libs 目錄,就需要這樣宣告。我試過,但格式略有偏差,build.gradle 會報錯,改成下面這樣就沒問題了:

sourceSets {
main {
jniLibs.srcDir 'libs'
}

3. 添加 service。定位服務是作為 Service 開啟的,需要加進 AndroidManifest.xml:

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

4. 添加權限。上一篇已把後續所需權限一併包含進來,這裡就不再附上。

二、熟悉主要的定位類

1. LocationClient

這是定位服務的類,相當於定義一個具備定位服務的客戶端,且只能在主線程中啟動。它兩個建構子都需要傳入全局的 context,建議用 getApplicationContext 傳入。

這兩個建構子的差別在於:創建過程之前是否已定義了配置定位參數的 LocationClientOption。若採用第一個建構方法,後面就需要用 setLocOption 來補充。常用方法有 setLocOption、start、stop、registerLocationListener、registerNotify、requestNotifyLocation、removeNotifyEvent 以及 requestOfflineLocation 等。一般在建構並設定參數後,就可以呼叫 start 啟動定位服務。

2. LocationClientOption

用來配置定位 SDK 的各種參數。常用方法有 setCoorType(設定座標類型)、setIsNeedAddress(是否需要地址資訊)、setIsNeedLocationPoiList(是否返回 POI 資訊)、setLocationMode(設定定位模式)、setOpenGps(是否開啟 GPS)、setScanSpan(設定掃描間隔,單位毫秒)等。常用的設定包括定位模式、座標系、掃描時間間隔、返回地址結果、是否開啟 GPS。

定位模式(預設高精度):

(1)高精度定位模式(Hight_Accuracy):同時使用網路定位與 GPS 定位,優先返回精度最高的結果。

(2)低功耗定位模式(Battery_Saving):不使用 GPS,只用網路定位(WiFi 和基站)。

(3)僅用裝置定位模式(Device_Sensors):不使用網路定位,只用 GPS;但此模式不支援室內定位。

定位座標系(建議採用 bd09ll,百度地圖用自己的座標比較準):

(1)國測局經緯度座標系:gcj02;
(2)百度墨卡托座標系:bd09;
(3)百度經緯度座標系:bd09ll。

掃描時間間隔(單位 ms):當 <1000(1s)時,定時定位無效。

3. BDLocationListener 定位請求回呼介面

設定完定位參數後,需要設定監聽事件,主要是重寫 onReceiveLocation(BDLocation location) 函數,實現對 location 結果的處理。

4. BDLocation

內部封裝了經緯度、半徑等屬性。常用方法有 getAddrStr(詳細地址)、getCity、getCountry、getLatitude、getLongitude、getProvince、getStreet、getTime、getRadius(定位精度)、getDirection(行進方向)等。常獲取的資訊包括經緯度、精度範圍與行進方向。

5. MyLocationData 定位數據包

前面的類完成了地圖的定位與監聽響應,若要把它顯示在地圖上,就需要構造定位數據包。透過 MyLocationData.Builder 的 accuracy、direction、latitude、longitude、speed、satellitesNum 等方法設定數據,再呼叫 build() 生成定位數據對象。

6. MyLocationConfiguration 配置定位圖層

建構子為 MyLocationConfiguration(LocationMode mode, boolean enableDirection, BitmapDescriptor customMarker),用來決定定位圖層的顯示方式(LocationMode)。

有了上面這幾個基本的類,就能簡單地使用定位服務了。若還有其他需要查找的類,也可以到官網搜尋:https://wiki.lbsyun.baidu.com/cms/androidloc/doc/v6_0_3/doc/index.html

核心程式碼附上:


mLocClient = new LocationClient(this);//定義定位客戶端
mLocClient.registerLocationListener(myListener);//設置監聽事件
LocationClientOption option = new LocationClientOption();//設置參數
option.setOpenGps(true); // 打開gps
option.setCoorType("bd09ll"); // 設置坐標類型
option.setScanSpan(1000);//設施掃描間隔時間
mLocClient.setLocOption(option);//將參數添加進客戶端
mLocClient.start();//開啟客戶端
public class MyLocationListenner implements BDLocationListener {

@Override
public void onReceiveLocation(BDLocation location) {
// map view 銷毀後不在處理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
.direction(100)
.latitude(location.getLatitude())
.longitude(location.getLongitude())
.build();
mBaiduMap.setMyLocationData(locData);
 if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
//mToast = Toast.makeText(LocationDemo.this, "第一次位置:"+ll.latitude +"|"+ ll.longitude, Toast.LENGTH_SHORT);
//mToast.show();
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved