百度地图添加方向传感器

xiaoxiao2021-02-27  106

这几天工作需要集成百度地图添加方向传感器代码如下

1、首先在需要定位的Activity或者Fragemnt中初始化监听器

我是在Activity中onCreate()中添加的

/** * 方向传感器监听回调 */ private void initOritationListener() { myOrientationListener = new MyOrientationListener( getActivity()); myOrientationListener .setOnOrientationListener(new MyOrientationListener.OnOrientationListener() { @Override public void onOrientationChanged(float x) { mXDirection = (int) x; String mAccracy = Shared.readString(getActivity(), "sAccracy"); //精度 if (!TextUtils.isEmpty(mAccracy) && !mAccracy.equals("")) { mCurrentAccracy = Float.valueOf(mAccracy); } //经纬度 if (!Shared.readString(getActivity(), "sLatitude").equals("") && !Shared.readString(getActivity(), "sLongitude").equals("")) { mCurrentLantitude = Double.parseDouble(Shared.readString(getActivity(), "sLatitude")); mCurrentLongitude = Double.parseDouble(Shared.readString(getActivity(), "sLongitude")); } // 构造定位数据 MyLocationData locData = new MyLocationData.Builder() .accuracy(mCurrentAccracy) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(mXDirection) .latitude(mCurrentLantitude) .longitude(mCurrentLongitude).build(); // 设置定位数据 mBaiduMap.setMyLocationData(locData); // 设置自定义图标 BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory .fromResource(R.mipmap.userstation); MyLocationConfiguration config = new MyLocationConfiguration( MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker); mBaiduMap.setMyLocationConfigeration(config); } }); } 2、在onStart()和onStop()中进行传感器监听生命周期控制

@Override public void onStart() { //确保能够进行定位 if (mBaiduMap!=null){ mBaiduMap.setMyLocationEnabled(true); } myOrientationListener.start(); super.onStart(); } @Override public void onStop() { if (mBaiduMap!=null){ mBaiduMap.setMyLocationEnabled(false); } myOrientationListener.stop(); clearTask(); super.onStop(); } 3、 OrientationListener的回调 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 Context context; private SensorManager sensorManager; private Sensor sensor; private float lastX; private OnOrientationListener onOrientationListener; public MyOrientationListener(Context context) { this.context = context; } // 开始 public void start() { // 获得传感器管理器 sensorManager = (SensorManager) context .getSystemService(Context.SENSOR_SERVICE); if (sensorManager != null) { // 获得方向传感器 sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); } // 注册 if (sensor != null) { sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI); } } // 停止检测 public void stop() { sensorManager.unregisterListener(this); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @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) { onOrientationListener.onOrientationChanged(x); } lastX = x; } } public void setOnOrientationListener(OnOrientationListener onOrientationListener) { this.onOrientationListener = onOrientationListener; } public interface OnOrientationListener { void onOrientationChanged(float x); } }

转载请注明原文地址: https://www.6miu.com/read-6052.html

最新回复(0)