【问题标题】:Android: How to get the direction the device are currently facing towards similar to Google MapAndroid:如何获取设备当前面向的方向,类似于谷歌地图
【发布时间】:2021-07-21 10:17:30
【问题描述】:

我目前正在使用定位服务来获取用户的位置以及通过 Geocoder 获取地址。

我存储了这些数据以供以后显示。同时,我想保存应用程序获取位置时他们所面对的方向。

旅行时谷歌地图是否使用指南针 API 来处理它,或者是否有任何其他我错过的 android 库。

我怀疑它是否使用了 Compass Api,因为当我尝试安装 Compass 应用程序时,我的手机没有传感器。

【问题讨论】:

    标签: android google-maps sensors


    【解决方案1】:

    你可以使用Positions Sensors:

    public class SensorActivity extends Activity implements SensorEventListener {
    
        private SensorManager sensorManager;
        private final float[] accelerometerReading = new float[3];
        private final float[] magnetometerReading = new float[3];
    
        private final float[] rotationMatrix = new float[9];
        private final float[] orientationAngles = new float[3];
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // Do something here if sensor accuracy changes.
            // You must implement this callback in your code.
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            // Get updates from the accelerometer and magnetometer at a constant rate.
            // To make batch operations more efficient and reduce power consumption,
            // provide support for delaying updates to the application.
            //
            // In this example, the sensor reporting delay is small enough such that
            // the application receives an update before the system checks the sensor
            // readings again.
            Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            if (accelerometer != null) {
                sensorManager.registerListener(this, accelerometer,
                    SensorManager.SENSOR_DELAY_NORMAL, SensorManager.SENSOR_DELAY_UI);
            }
            Sensor magneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
            if (magneticField != null) {
                sensorManager.registerListener(this, magneticField,
                    SensorManager.SENSOR_DELAY_NORMAL, SensorManager.SENSOR_DELAY_UI);
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
    
            // Don't receive any more updates from either sensor.
            sensorManager.unregisterListener(this);
        }
    
        // Get readings from accelerometer and magnetometer. To simplify calculations,
        // consider storing these readings as unit vectors.
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
              System.arraycopy(event.values, 0, accelerometerReading,
                  0, accelerometerReading.length);
            } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                System.arraycopy(event.values, 0, magnetometerReading,
                    0, magnetometerReading.length);
            }
        }
    
        // Compute the three orientation angles based on the most recent readings from
        // the device's accelerometer and magnetometer.
        public void updateOrientationAngles() {
            // Update rotation matrix, which is needed to update orientation angles.
            SensorManager.getRotationMatrix(rotationMatrix, null,
                accelerometerReading, magnetometerReading);
    
            // "rotationMatrix" now has up-to-date information.
    
            SensorManager.getOrientation(rotationMatrix, orientationAngles);
    
            // "orientationAngles" now has up-to-date information.
        }
    }
    

    加速度计 (7.3.1.) 和磁力计 (7.3.2.) 在 Android Compatibility Definition 中“强烈推荐”,并且很可能存在于您的设备中。

    【讨论】:

    • 您好,感谢您的帮助。我意识到我的手机没有磁力计。我的设备只有加速度计、接近传感器。
    • @LittleFunny 您可以使用陀螺仪检测北向(link1link2 但它需要一些特殊条件,例如设备旋转)。
    • @LittleFunny 或者你可以提供empiric estimate of North direction的UI:可以根据当地时间和太阳的方向来确定。
    猜你喜欢
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多