更改之前图标:
关键代码
/*** 设置定位图层配置信息,只有先允许定位图层后设置定位图层配置信息才会生效* customMarker用户自定义定位图标* enableDirection是否允许显示方向信息* locationMode定位图层显示方式*/View view = LayoutInflater.from(context).inflate(R.layout.marker_location_layout, null);BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromView(view);mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, bitmapDescriptor));
更改之后:
箭头随着屏幕转动:
转动监听类:
package com.chinatower.fghd.customer.home;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 SensorManager mSensorManager;private Context mContext;private Sensor mSensor;private float lastX;private OnOrientationListener mOnOrientationListener;public void setmOnOrientationListener(OnOrientationListener mOnOrientationListener) {this.mOnOrientationListener = mOnOrientationListener;}public MyOrientationListener(Context context) {this.mContext = context;}public void star() {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);}@Overridepublic 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) {if (mOnOrientationListener != null) {mOnOrientationListener.onOrientationChanged(x);}}lastX = x;}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}public interface OnOrientationListener {void onOrientationChanged(float x);}}
在使用的地方依次加入如下代码:
private MyOrientationListener myOrientationListener;..........myOrientationListener = new MyOrientationListener(context);myOrientationListener.setmOnOrientationListener(new MyOrientationListener.OnOrientationListener() {@Overridepublic void onOrientationChanged(float x) {if(myLocationData == null ){return;}MyLocationData.Builder builder=new MyLocationData.Builder();builder.longitude(myLocationData.longitude).latitude(myLocationData.latitude).direction(x);myLocationData=builder.build();mBaiduMap.setMyLocationData(myLocationData); //手机方向改变实时改变图标指向}});myOrientationListener.star();