您的位置:首页 > 移动开发 > Android开发

android 百度地图系列之结合方向传感器的地图定位

2016-06-30 19:06 591 查看
因为这是关于百度地图的系列博客,本文章的百度地图定位在上一篇(android 百度地图系列之地图初始化及定位)中已经详细介绍过,就在上篇博客的基础上,添加方向传感器来使定位图标显示自己在地图上的方向。

首先需要一张方向朝上的定位标志图。(CSDN上传图片失败,下次修改)

注意:一定要方向朝上,要不然方向会有偏差



在自定义定位图标的时候需要bitmapDescriptor,所以需要将bitmapDescriptor获取到

//初始化图标
bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.arrow);


并在MyLocationListener的onReceiveLocation方法中添加自定义的定位图标显示方式。

//配置定位图层显示方式,使用自己的定位图标
MyLocationConfiguration configuration = new MyLocationConfiguration(LocationMode.NORMAL, true, bitmapDescriptor);
mBaiduMap.setMyLocationConfigeration(configuration);


这样,自定义的图标就显示出来了,现在需要与方向传感器结合来实现带方向的定位图标,并在旋转手机的时候,自动旋转图标。

现在需要一个传感器的监听,定义一个了类实现SensorEventListener,重写onSensorChanged()和onAccuracyChanged()方法。其中onAccuracyChanged()监听精度改变不需要,只要通过onSensorChanged()监听x轴方向改变就满足需要。

直接上代码,注释很清楚

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* 方向传感器监听
* @author wangjian
*
*/
public class MyOrientationListener implements SensorEventListener{
//传感器管理者
private SensorManager mSensorManager;
//上下文
private Context mContext;
//传感器
private Sensor mSensor;

//方向传感器有三个坐标,现在只关注X
private float mLastX;
//构造函数
public MyOrientationListener(Context context) {
this.mContext = context;
}
//开始监听
@SuppressWarnings("deprecation")
public void start(){
//获得传感器管理者
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
if(mSensorManager!=null){//是否支持
//获得方向传感器
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
if(mSensor!=null){//如果手机有方向传感器,精度可以自己去设置,注册方向传感器
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
}
}
//结束监听
public void stop(){
//取消注册的方向传感器
mSensorManager.unregisterListener(this);
}
//传感器发生改变时
@SuppressWarnings("deprecation")
@Override
public void onSensorChanged(SensorEvent event) {
//判断返回的传感器类型是不是方向传感器
if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
//只获取x的值
float x = event.values[SensorManager.DATA_X];
//为了防止经常性的更新
if(Math.abs(x-mLastX)>1.0){
if(onOrientationListener!=null){
onOrientationListener.onOrientationChanged(x);
}
}
mLastX = x;
}
}

//当传感器精度发生改变,当前不用
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private OnOrientationListener onOrientationListener;

public void setOnOrientationListener(OnOrientationListener onOrientationListener) {
this.onOrientationListener = onOrientationListener;
}

//回掉方法
public interface OnOrientationListener{
void onOrientationChanged(float x);
}
}


开始在定位中实现方向传感器的结合。

将initMyLoc()添加到onCreate()中。

 private void initMyLoc() {
//初始化图标 bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.arrow);
//方向传感器监听
myOrientationListener = new MyOrientationListener(this);
myOrientationListener.setOnOrientationListener(new OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
//将获取的x轴方向赋值给全局变量
mLastX = x;
}
});
}


修改获取location信息时的配置,添加旋转角度,这样每次定位的时候就能旋转角度了。

//自定义的定位监听
private class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
//将获取的location信息给百度map
MyLocationData data = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360,mLastX就是获取到的方向传感器传来的x轴数值
.direction(mLastX)
.latitude(location.getLatitude())
.longitude(location.getLongitude())
.build();
mBaiduMap.setMyLocationData(data);
//更新经纬度
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
//配置定位图层显示方式,使用自己的定位图标
//LocationMode定位模式有三种:普通模式,跟随模式,罗盘模式,在这使用普通模式
MyLocationConfiguration configuration = new MyLocationConfiguration(LocationMode.NORMAL, true, bitmapDescriptor);
mBaiduMap.setMyLocationConfigeration(configuration);
if(isFirstLocation){
//获取经纬度
LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
MapStatusUpdate status = MapStatusUpdateFactory.newLatLng(ll);
//mBaiduMap.setMapStatus(status);//直接到中间
mBaiduMap.animateMapStatus(status);//动画的方式到中间
isFirstLocation = false;
showInfo("位置:" + location.getAddrStr());
}
}
}


最后展示一下:

ps:csdn上传图片失败。。不知道怎么回事,下回上传一下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息