轉載請标明出處:http://blog.csdn.net/lmj623565791/article/details/37730469
在上一篇部落格中,我們成功把地圖導入了我們的項目。本篇我們準備為地圖添加:第一,定位功能;第二,與方向傳感器結合,通過旋轉手機進行道路的方向确認。有了這兩個功能,地圖已經可以為我服務了~~~~
效果圖:

好了,可以代碼,為了友善,我把所有的按鈕都放到了menu菜單中。
1、初次啟動定位
[java] view plain copy
- 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:
[java] view plain copy
- 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);
- }
- }
- }
可以看到,我們初始化了定位的參數,設定了定位的監聽器,每隔1s會進行一次定位,應用打開時,第一定位,會把地圖中心設定目前使用者位置。
定位也是比較耗電的,是以我們在onStart中開啟定位,在onStop中關閉定位~~這樣應用最小化時就不會一直在哪GPS請求定位了,使用者要是看你app一直在那定位,估計馬上就被解除安裝了~
[java] view plain copy
- @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
[html] view plain copy
- <service
- android:name="com.baidu.location.f"
- android:enabled="true"
- android:process=":remote" >
- <intent-filter>
- <action android:name="com.baidu.location.service_v2.2" >
- </action>
- </intent-filter>
- </service>
現在基本的定位功能已經實作了~不過我們還需要添加點選定位按鈕和方向傳感器
2、我的位置
點選我的位置菜單會調用center2myLoc方法。
[java] view plain copy
- case R.id.id_menu_map_myLoc:
- center2myLoc();
- break;
[java] view plain copy
- private void center2myLoc()
- {
- LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude);
- MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
- mBaiduMap.animateMapStatus(u);
- }
很簡單,我們在定位的監聽器中已經儲存了最近一次的定位經緯度,是以隻需要點選時,把地圖移動到相應的位置即可。
3、內建方向傳感器
首先是封裝的方向傳感器的類MyOrientationListener.java
[java] view plain copy
- 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中初始化方向傳感器
[java] view plain copy
- 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中分别開啟和關閉方向傳感器。
對于旋轉手機确定方向,實際上利用了
[java] view plain copy
- new MyLocationData.Builder()
- //此處設定開發者擷取到的方向資訊,順時針0-360 .direction(mXDirection)
隻需要把x方向的角度設定即可~~~是不是很簡單~~~
好了,介紹完畢了,關閉地圖樣式的切換,以及跟随、羅盤等模式的切換就不介紹了,大家自己看下源碼~~
源碼點選下載下傳
注:開發者key需要換成自己申請的,不清楚申請的請看第一篇部落格的。