android 百度地圖系列之結合方向感測器的地圖定位
阿新 • • 發佈:2019-02-05
因為這是關於百度地圖的系列部落格,本文章的百度地圖定位在上一篇(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上傳圖片失敗。。不知道怎麼回事,下回上傳一下