300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > android 百度地图自定义定位小箭头图标 并随着手机方向转动

android 百度地图自定义定位小箭头图标 并随着手机方向转动

时间:2024-01-17 12:19:11

相关推荐

android 百度地图自定义定位小箭头图标 并随着手机方向转动

更改之前图标:

关键代码

/*** 设置定位图层配置信息,只有先允许定位图层后设置定位图层配置信息才会生效* 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();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。