Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37730469

在上一篇博客中,我们成功把地图导入了我们的项目。本篇我们准备为地图添加:第一,定位功能;第二,与方向传感器结合,通过旋转手机进行道路的方向确认。有了这两个功能,地图已经可以为我服务了~~~~

效果图:

好了,可以代码,为了方便,我把所有的按钮都放到了menu菜单中。

1、初次启动定位
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.      * 定位的客户端 
  3.      */  
  4.     private LocationClient mLocationClient;  
  5.     /** 
  6.      * 定位的监听器 
  7.      */  
  8.     public MyLocationListener mMyLocationListener;  
  9.     /** 
  10.      * 当前定位的模式 
  11.      */  
  12.     private LocationMode mCurrentMode = LocationMode.NORMAL;  
  13.     /*** 
  14.      * 是否是第一次定位 
  15.      */  
  16.     private volatile boolean isFristLocation = true;  
  17.     /** 
  18.      * 初始化定位相关代码 
  19.      */  
  20.     private void initMyLocation()  
  21.     {  
  22.         // 定位初始化  
  23.         mLocationClient = new LocationClient(this);  
  24.         mMyLocationListener = new MyLocationListener();  
  25.         mLocationClient.registerLocationListener(mMyLocationListener);  
  26.         // 设置定位的相关配置  
  27.         LocationClientOption option = new LocationClientOption();  
  28.         option.setOpenGps(true);// 打开gps  
  29.         option.setCoorType("bd09ll"); // 设置坐标类型  
  30.         option.setScanSpan(1000);  
  31.         mLocationClient.setLocOption(option);  
  32.     }  


然后是定位的监听器MyLocationListener:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.      * 实现实位回调监听 
  3.      */  
  4.     public class MyLocationListener implements BDLocationListener  
  5.     {  
  6.         @Override  
  7.         public void onReceiveLocation(BDLocation location)  
  8.         {  
  9.   
  10.             // map view 销毁后不在处理新接收的位置  
  11.             if (location == null || mMapView == null)  
  12.                 return;  
  13.             // 构造定位数据  
  14.             MyLocationData locData = new MyLocationData.Builder()  
  15.                     .accuracy(location.getRadius())  
  16.                     // 此处设置开发者获取到的方向信息,顺时针0-360  
  17.                     .direction(mXDirection).latitude(location.getLatitude())  
  18.                     .longitude(location.getLongitude()).build();  
  19.             mCurrentAccracy = location.getRadius();  
  20.             // 设置定位数据  
  21.             mBaiduMap.setMyLocationData(locData);  
  22.             mCurrentLantitude = location.getLatitude();  
  23.             mCurrentLongitude = location.getLongitude();  
  24.             // 设置自定义图标  
  25.             BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory  
  26.                     .fromResource(R.drawable.navi_map_gps_locked);  
  27.             MyLocationConfigeration config = new MyLocationConfigeration(  
  28.                     mCurrentMode, true, mCurrentMarker);  
  29.             mBaiduMap.setMyLocationConfigeration(config);  
  30.             // 第一次定位时,将地图位置移动到当前位置  
  31.             if (isFristLocation)  
  32.             {  
  33.                 isFristLocation = false;  
  34.                 LatLng ll = new LatLng(location.getLatitude(),  
  35.                         location.getLongitude());  
  36.                 MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);  
  37.                 mBaiduMap.animateMapStatus(u);  
  38.             }  
  39.         }  
  40.   
  41.     }  

可以看到,我们初始化了定位的参数,设置了定位的监听器,每隔1s会进行一次定位,应用打开时,第一定位,会把地图中心设置当前用户位置。
定位也是比较耗电的,所以我们在onStart中开启定位,在onStop中关闭定位~~这样应用最小化时就不会一直在哪GPS请求定位了,用户要是看你app一直在那定位,估计马上就被卸载了~

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @Override  
  2.     protected void onStart()  
  3.     {  
  4.         // 开启图层定位  
  5.         mBaiduMap.setMyLocationEnabled(true);  
  6.         if (!mLocationClient.isStarted())  
  7.         {  
  8.             mLocationClient.start();  
  9.         }  
  10.         // 开启方向传感器  
  11.         myOrientationListener.start();  
  12.         super.onStart();  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void onStop()  
  17.     {  
  18.         // 关闭图层定位  
  19.         mBaiduMap.setMyLocationEnabled(false);  
  20.         mLocationClient.stop();  
  21.   
  22.         // 关闭方向传感器  
  23.         myOrientationListener.stop();  
  24.         super.onStop();  
  25.     }  


上面的传感器的代码,一会就会介绍~

记得在AndroidManifest.xml配一个service

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <service  
  2.           android:name="com.baidu.location.f"  
  3.           android:enabled="true"  
  4.           android:process=":remote" >  
  5.           <intent-filter>  
  6.               <action android:name="com.baidu.location.service_v2.2" >  
  7.               </action>  
  8.           </intent-filter>  
  9.       </service>  


现在基本的定位功能已经实现了~不过我们还需要添加点击定位按钮和方向传感器

2、我的位置

点击我的位置菜单会调用center2myLoc方法。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. case R.id.id_menu_map_myLoc:  
  2.         center2myLoc();  
  3.         break;  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.      * 地图移动到我的位置,此处可以重新发定位请求,然后定位;  
  3.      * 直接拿最近一次经纬度,如果长时间没有定位成功,可能会显示效果不好 
  4.      */  
  5.     private void center2myLoc()  
  6.     {  
  7.         LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude);  
  8.         MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);  
  9.         mBaiduMap.animateMapStatus(u);  
  10.     }  


很简单,我们在定位的监听器中已经保存了最近一次的定位经纬度,所以只需要点击时,把地图移动到相应的位置即可。

3、集成方向传感器

首先是封装的方向传感器的类MyOrientationListener.java

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package com.zhy.zhy_baidu_ditu_demo00;  
  2.   
  3. import android.content.Context;  
  4. import android.hardware.Sensor;  
  5. import android.hardware.SensorEvent;  
  6. import android.hardware.SensorEventListener;  
  7. import android.hardware.SensorManager;  
  8.   
  9. public class MyOrientationListener implements SensorEventListener  
  10. {  
  11.   
  12.     private Context context;  
  13.     private SensorManager sensorManager;  
  14.     private Sensor sensor;  
  15.       
  16.     private float lastX ;   
  17.       
  18.     private OnOrientationListener onOrientationListener ;   
  19.   
  20.     public MyOrientationListener(Context context)  
  21.     {  
  22.         this.context = context;  
  23.     }  
  24.   
  25.     // 开始  
  26.     public void start()  
  27.     {  
  28.         // 获得传感器管理器  
  29.         sensorManager = (SensorManager) context  
  30.                 .getSystemService(Context.SENSOR_SERVICE);  
  31.         if (sensorManager != null)  
  32.         {  
  33.             // 获得方向传感器  
  34.             sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);  
  35.         }  
  36.         // 注册  
  37.         if (sensor != null)  
  38.         {//SensorManager.SENSOR_DELAY_UI  
  39.             sensorManager.registerListener(this, sensor,  
  40.                     SensorManager.SENSOR_DELAY_UI);  
  41.         }  
  42.   
  43.     }  
  44.   
  45.     // 停止检测  
  46.     public void stop()  
  47.     {  
  48.         sensorManager.unregisterListener(this);  
  49.     }  
  50.   
  51.     @Override  
  52.     public void onAccuracyChanged(Sensor sensor, int accuracy)  
  53.     {  
  54.           
  55.     }  
  56.   
  57.     @Override  
  58.     public void onSensorChanged(SensorEvent event)  
  59.     {  
  60.         // 接受方向感应器的类型    
  61.         if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)    
  62.         {    
  63.             // 这里我们可以得到数据,然后根据需要来处理    
  64.             float x = event.values[SensorManager.DATA_X];    
  65.               
  66.             if( Math.abs(x- lastX) > 1.0 )  
  67.             {  
  68.                 onOrientationListener.onOrientationChanged(x);  
  69.             }  
  70. //            Log.e("DATA_X", x+"");  
  71.             lastX = x ;   
  72.               
  73.         }    
  74.     }  
  75.       
  76.     public void setOnOrientationListener(OnOrientationListener onOrientationListener)  
  77.     {  
  78.         this.onOrientationListener = onOrientationListener ;  
  79.     }  
  80.       
  81.       
  82.     public interface OnOrientationListener   
  83.     {  
  84.         void onOrientationChanged(float x);  
  85.     }  
  86.   
  87. }  


在onCreate中初始化方向传感器

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.      * 初始化方向传感器 
  3.      */  
  4.     private void initOritationListener()  
  5.     {  
  6.         myOrientationListener = new MyOrientationListener(  
  7.                 getApplicationContext());  
  8.         myOrientationListener  
  9.                 .setOnOrientationListener(new OnOrientationListener()  
  10.                 {  
  11.                     @Override  
  12.                     public void onOrientationChanged(float x)  
  13.                     {  
  14.                         mXDirection = (int) x;  
  15.   
  16.                         // 构造定位数据  
  17.                         MyLocationData locData = new MyLocationData.Builder()  
  18.                                 .accuracy(mCurrentAccracy)  
  19.                                 // 此处设置开发者获取到的方向信息,顺时针0-360  
  20.                                 .direction(mXDirection)  
  21.                                 .latitude(mCurrentLantitude)  
  22.                                 .longitude(mCurrentLongitude).build();  
  23.                         // 设置定位数据  
  24.                         mBaiduMap.setMyLocationData(locData);  
  25.                         // 设置自定义图标  
  26.                         BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory  
  27.                                 .fromResource(R.drawable.navi_map_gps_locked);  
  28.                         MyLocationConfigeration config = new MyLocationConfigeration(  
  29.                                 mCurrentMode, true, mCurrentMarker);  
  30.                         mBaiduMap.setMyLocationConfigeration(config);  
  31.   
  32.                     }  
  33.                 });  
  34.     }  


最后在onStart和onStop中分别开启和关闭方向传感器。

对于旋转手机确定方向,实际上利用了

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. new MyLocationData.Builder()              
  2. //此处设置开发者获取到的方向信息,顺时针0-360                                                                                                .direction(mXDirection)  

只需要把x方向的角度设置即可~~~是不是很简单~~~

好了,介绍完毕了,关闭地图样式的切换,以及跟随、罗盘等模式的切换就不介绍了,大家自己看下源码~~

源码点击下载
注:开发者key需要换成自己申请的,不清楚申请的请看第一篇博客的。

原文地址:https://www.cnblogs.com/dongweiq/p/3945338.html