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

百度地圖 SDK v3.0.0 實戰(二):定位與方向感測器整合

編輯:關於 Android 程式設計

在上一篇 Blog 中,我們成功地把地圖導入專案。本篇則要為地圖加上兩個實用功能:第一是定位;第二是與方向感測器結合,透過旋轉手機來確認道路方向。有了這兩個功能,地圖就能真正為我們所用囉~

效果圖:

為了方便示範,我把所有按鈕都放進了 menu 選單中。首先是定位相關的初始化程式碼:

/** * 定位的客戶端 */ private LocationClient mLocationClient; /** * 定位的監聽器 */ public MyLocationListener mMyLocationListener; /** * 當前定位的模式 */ private LocationMode mCurrentMode = LocationMode.NORMAL; /*** * 是否是第一次定位 */ private volatile boolean isFristLocation = true; /** * 初始化定位相關代碼 */ private void initMyLocation() { // 定位初始化 mLocationClient = new LocationClient(this); mMyLocationListener = new MyLocationListener(); mLocationClient.registerLocationListener(mMyLocationListener); // 設置定位的相關配置 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打開gps option.setCoorType("bd09ll"); // 設置坐標類型 option.setScanSpan(1000); mLocationClient.setLocOption(option); }

接著是定位的監聽器 MyLocationListener:

/** * 實現實位回調監聽 */ public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 銷毀後不在處理新接收的位置 if (location == null || mMapView == null) return; // 構造定位數據 MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此處設置開發者獲取到的方向信息,順時針0-360 .direction(mXDirection).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mCurrentAccracy = location.getRadius(); // 設置定位數據 mBaiduMap.setMyLocationData(locData); mCurrentLantitude = location.getLatitude(); mCurrentLongitude = location.getLongitude(); // 設置自定義圖標 BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.navi_map_gps_locked); MyLocationConfigeration config = new MyLocationConfigeration( mCurrentMode, true, mCurrentMarker); mBaiduMap.setMyLocationConfigeration(config); // 第一次定位時,將地圖位置移動到當前位置 if (isFristLocation) { isFristLocation = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(u); } } }

可以看到,我們初始化了定位參數、設定了定位監聽器,每隔 1 秒會進行一次定位;應用打開後的第一次定位,會把地圖中心移到當前使用者位置。

由於定位相當耗電,因此我們在 onStart 中開啟定位、在 onStop 中關閉定位——這樣當應用最小化時就不會一直在背景進行 GPS 定位請求了。使用者若發現你的 App 一直在定位,恐怕很快就會被卸載~

@Override protected void onStart() { // 開啟圖層定位 mBaiduMap.setMyLocationEnabled(true); if (!mLocationClient.isStarted()) { mLocationClient.start(); } // 開啟方向傳感器 myOrientationListener.start(); super.onStart(); } @Override protected void onStop() { // 關閉圖層定位 mBaiduMap.setMyLocationEnabled(false); mLocationClient.stop(); // 關閉方向傳感器 myOrientationListener.stop(); super.onStop(); }

別忘了在 AndroidManifest.xml 中配置一個 service:

    

至此,基本的定位功能已經完成~不過我們還需要加入「點擊回到我的位置」與方向感測器。

2. 我的位置

點擊「我的位置」選單會呼叫 center2myLoc 方法:

 case R.id.id_menu_map_myLoc: center2myLoc(); break;
/** * 地圖移動到我的位置,此處可以重新發定位請求,然後定位; * 直接拿最近一次經緯度,如果長時間沒有定位成功,可能會顯示效果不好 */ private void center2myLoc() { LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(u); }

很簡單——我們在定位監聽器中已經保存了最近一次的經緯度,所以點擊時只需把地圖移動到對應位置即可。

3. 整合方向感測器

首先是封裝的方向感測器類別 MyOrientationListener.java:

package com.zhy.zhy_baidu_ditu_demo00; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class MyOrientationListener implements SensorEventListener { private Context context; private SensorManager sensorManager; private Sensor sensor; private float lastX ; private OnOrientationListener onOrientationListener ; public MyOrientationListener(Context context) { this.context = context; } // 開始 public void start() { // 獲得傳感器管理器 sensorManager = (SensorManager) context .getSystemService(Context.SENSOR_SERVICE); if (sensorManager != null) { // 獲得方向傳感器 sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); } // 注冊 if (sensor != null) {//SensorManager.SENSOR_DELAY_UI sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI); } } // 停止檢測 public void stop() { sensorManager.unregisterListener(this); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { // 接受方向感應器的類型 if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { // 這裡我們可以得到數據,然後根據需要來處理 float x = event.values[SensorManager.DATA_X]; if( Math.abs(x- lastX) > 1.0 ) { onOrientationListener.onOrientationChanged(x); } // Log.e("DATA_X", x+""); lastX = x ; } } public void setOnOrientationListener(OnOrientationListener onOrientationListener) { this.onOrientationListener = onOrientationListener ; } public interface OnOrientationListener { void onOrientationChanged(float x); } } 

接著在 onCreate 中初始化方向感測器:

/** * 初始化方向傳感器 */ private void initOritationListener() { myOrientationListener = new MyOrientationListener( getApplicationContext()); myOrientationListener .setOnOrientationListener(new OnOrientationListener() { @Override public void onOrientationChanged(float x) { mXDirection = (int) x; // 構造定位數據 MyLocationData locData = new MyLocationData.Builder() .accuracy(mCurrentAccracy) // 此處設置開發者獲取到的方向信息,順時針0-360 .direction(mXDirection) .latitude(mCurrentLantitude) .longitude(mCurrentLongitude).build(); // 設置定位數據 mBaiduMap.setMyLocationData(locData); // 設置自定義圖標 BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.navi_map_gps_locked); MyLocationConfigeration config = new MyLocationConfigeration( mCurrentMode, true, mCurrentMarker); mBaiduMap.setMyLocationConfigeration(config); } }); }

最後在 onStart 與 onStop 中分別開啟與關閉方向感測器。

至於「旋轉手機確認方向」,實際上是利用了:

new MyLocationData.Builder() //此處設置開發者獲取到的方向信息,順時針0-360 .direction(mXDirection)

只要把 x 方向的角度設定進去即可~是不是相當簡單呢~

關於地圖樣式切換,以及跟隨、羅盤等模式的切換就不在此贅述,大家可以自行參考原始碼。

原始碼可點擊下載。

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