定位方向加上手机传感方向(放在博客方便自己ctrol+c)

package com.vastweb.securitypatrolmvp.util;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

import com.tencent.tencentmap.mapsdk.maps.LocationSource;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;

/**
 * @author : Life
 * onCreate DateTime 2021/3/23  : 10:31
 * in order to : 辅助定位图标准确的显示指向;利用手机传感器
 **/

public class SensorEventHelper implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mSensor;
    private long lastTime = 0;
    private final int TIME_SENSOR = 100;
    private float mAngle;
    private Context mContext;
    private Marker mMarker;
    private LocationSource.OnLocationChangedListener mChangedListener;
    private Location mLocation;

    public SensorEventHelper(Context context) {
        mContext = context;
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        LogUtil.info(this.getClass().getName() + "进入传感器控制");
    }

    public void registerSensorListener() {
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void unRegisterSensorListener() {
        mSensorManager.unregisterListener(this, mSensor);
    }

    public void setCurrentMarker(Marker marker) {
        mMarker = marker;
    }

    public void setLocationSource(Location location) {
        mLocation = location;
    }

    public void setLocationListener(LocationSource.OnLocationChangedListener listener) {
        mChangedListener = listener;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (System.currentTimeMillis() - lastTime < TIME_SENSOR) {
            return;
        }
        LogUtil.info(this.getClass().getName() + "传感反馈");
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ORIENTATION: {
                LogUtil.info(this.getClass().getName() + "传感反馈:方向类型");
                float x = event.values[0];
                x += getScreenRotationOnPhone(mContext);
                x %= 360.0F;
                if (x > 180.0F) x -= 360.0F;
                else if (x < -180.0F) x += 360.0F;

                if (Math.abs(mAngle - x) < 3.0f) {
                    break;
                }
                mAngle = Float.isNaN(x) ? 0 : x;
                if (mMarker != null) {
                    mMarker.setRotation(360 - mAngle);
                }
                LogUtil.info(this.getClass().getName() + "传感反馈:方向类型--" + (mChangedListener != null) + ":" + (mLocation != null));
                if (mChangedListener != null && mLocation != null) {
                    mLocation.setBearing(mAngle);
                    mChangedListener.onLocationChanged(mLocation);
                }
                lastTime = System.currentTimeMillis();
            }
        }

    }

    /**
     * 获取当前屏幕旋转角度
     *
     * @param context
     * @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏
     */
    public static int getScreenRotationOnPhone(Context context) {
        final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        switch (display.getRotation()) {
            case Surface.ROTATION_0:
                return 0;

            case Surface.ROTATION_90:
                return 90;

            case Surface.ROTATION_180:
                return 180;

            case Surface.ROTATION_270:
                return -90;
        }
        return 0;
    }
}

代码从网络复制整理,如有侵权,联系立马删除!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值