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);
    }


    @Override
    public 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;
        }
    }

    @Override
    public 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() {
    
    
            @Override
            public 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();

猜你喜欢

转载自blog.csdn.net/qq_38847655/article/details/125334494