El mapa de Android Baidu personaliza el ícono de flecha pequeña de posicionamiento y gira con la dirección del teléfono

Cambie el icono antes:
Insertar descripción de la imagen aquí

clave

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

Después del cambio:
Insertar descripción de la imagen aquí
la flecha gira con la pantalla:
rotación clase de escucha:

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

Agregue el siguiente código cuando se utilice:

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

Supongo que te gusta

Origin blog.csdn.net/qq_38847655/article/details/125334494
Recomendado
Clasificación