如何在没有磁力计的情况下检测磁北/方位角

How to detect magnetic north / azimuth without Magnetometer

我正在开发 Android 设备,该设备没有磁力计,但有旋转传感器(陀螺仪)、重力传感器和 GPS。我想通过这些传感器确定 真北/方位角 但不知道如何确定。没有磁力计,如何确定此设备的方向?显然,它以某种方式知道地图应用程序工作正常的北方位置。

更新:

您可以选择最适合您情况的 Rotation Vector Sensor

private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

你想要的坐标系如下:

  • X 定义为向量乘积 Y x Z。它与设备当前位置处的地面相切,大致指向东
  • Y 在设备当前位置与地面相切,并指向 地磁北极
  • Z 指向天空并垂直于地平面。


Android 使 很多 传感器可用

如果您特别希望使用陀螺仪 (Sensor.TYPE_GYROSCOPE),您可以这样做(摘自 Google example):

private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

public void onSensorChanged(SensorEvent event) {
      //...
}

但您可以尝试检查是否可以使用更高级别 API 以利用多个传感器

你应该看看 JavaCodeGeeks 的 simple example,他们利用默认的 Sensor.TYPE_ORIENTATION 构建一个 android 指南针

关于 Sensor.TYPE_ORIENTATION 的更多信息。 According to Google 您仍然可以使用设备方向,但是通过调用 getOrientation() 而不是直接作用于传感器。

Instead of using raw data from the orientation sensor, we recommend that you use the getRotationMatrix() method in conjunction with the getOrientation() method to compute orientation values, as shown in the following code sample. As part of this process, you can use the remapCoordinateSystem() method to translate the orientation values to your application's frame of reference.

坏消息是 getOrientation() 和旧的 Sensor.TYPE_ORIENTATION 至少部分依赖于磁力计

希望对您有所帮助!