How do I get the correct bearing (magnetic orientation) regardless of screen orientation?

后端 未结 3 712
栀梦
栀梦 2020-12-09 07:01

I want to get the current magnetic orientation regardless of the current screen orientation (landscape or portrait).

I found this example, but it\'s not orientation

3条回答
  •  情话喂你
    2020-12-09 07:45

    Sensor.TYPE_ORIENTATION is depreciated and only good if the device is flat. When using Sensor.TYPE_ORIENTATION, the bearing (azimuth) is the direction where the device Y-axis points. So if the device is held vertical, the direction where the Y-axis points using as the bearing does not make sense. It only make sense to calculate the direction where the back camera points. To find this direction you have to use Sensor.TYPE_MAGNETIC_FIELD and Sensor.TYPE_GRAVITY or Sensor.TYPE_ACCELEROMETER. If using Sensor.TYPE_ACCELEROMETER, you have to filter the accelerometer values.
    Using these sensors, you call getRotationMatrix and then remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR) before calling getOrientation. To get a stable direction you should keep a history of the direction and then calculate the average. For an implementation using TYPE_GRAVITY check Android getOrientation Azimuth gets polluted when phone is tilted

提交回复
热议问题