Android compass example

后端 未结 3 746
醉酒成梦
醉酒成梦 2020-12-14 04:44

I\'m searching for a compass example for Android. All I need to do is to get the correct bearing (in portrait & landscape mode).

I already found several samples,

相关标签:
3条回答
  • 2020-12-14 05:06

    Using a combination of Sensor.TYPE_ACCELEROMETER and Sensor.TYPE_MAGNETIC_FIELD and SensorManager.getOrientation() method doesn't give me precise values. The deprecated method Sensor.TYPE_ORIENTATION works well. :)

    0 讨论(0)
  • 2020-12-14 05:07

    Sensor.TYPE_ORIENTATION is deprecated.

    The documentation says you should use SensorManager.getOrientation() instead.

    So you should read from Sensor.TYPE_ACCELEROMETER as well as from Sensor.TYPE_MAGNETIC_FIELD, and then call SensorManager.getRotationMatrix() and finally SensorManager.getOrientation() which will return you the orientation of the phone.

    From there if you see this diagram it is trivial to get the phone's orientation. This is probably what your second example does, but I don't know because you didn't show me what it is.

    0 讨论(0)
  • 2020-12-14 05:07

    Thanks to Dan for his sample code.

    But there is just a little correction : change event.values to event.values.clone() according to this discussion.
    It works for me now after this modification.

    0 讨论(0)
提交回复
热议问题