Android 百度地圖 SDK v3.0.0 (二) 定位與結合方向傳感器
轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/37730469
在上一篇博客中,我們成功把地圖導入了我們的項目。本篇我們準備為地圖添加:第一,定位功能;第二,與方向傳感器結合,通過旋轉手機進行道路的方向確認。有了這兩個功能,地圖已經可以為我服務了~~~~
效果圖:
好了,可以代碼,為了方便,我把所有的按鈕都放到了menu菜單中。
1、初次啟動定位
/** * 定位的客戶端 */ 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); } } }可以看到,我們初始化了定位的參數,設置了定位的監聽器,每隔1s會進行一次定位,應用打開時,第一定位,會把地圖中心設置當前用戶位置。
定位也是比較耗電的,所以我們在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
<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方法。
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方向的角度設置即可~~~是不是很簡單~~~
好了,介紹完畢了,關閉地圖樣式的切換,以及跟隨、羅盤等模式的切換就不介紹了,大家自己看下源碼~~
源碼點擊下載
注:開發者key需要換成自己申請的,不清楚申請的請看第一篇博客的。
百度地圖相關博客視頻版本已經上線:Android中百度地圖的使用期待您的支持。
博主部分視頻已經上線,如果你不喜歡枯燥的文本,請猛戳(初錄,期待您的支持):
2、Android自定義控件實戰 打造Android流式布局和熱門標簽
百度地圖相關博客視頻版本已經上線:Android中百度地圖的使用期待您的支持。
博主部分視頻已經上線,如果你不喜歡枯燥的文本,請猛戳(初錄,期待您的支持):
2、Android自定義控件實戰 打造Android流式布局和熱門標簽
來自: http://blog.csdn.net//lmj623565791/article/details/37730469
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!