6

I have to display which direction the user is pointing the Android device.

I am using Sensor.TYPE_ACCELEROMETER, Sensor.TYPE_MAGNETIC_FIELD to get the azimuth, pitch, roll. But I am to able to figure out how to get directions (North, south, east , west,...) from this.

Please help

Thanks

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Android_enthusiast
  • 863
  • 5
  • 22
  • 41

2 Answers2

2

To point the north you can calculate a rotation in degrees :

float rotation = -azimut * 360 / (2 * 3.14159f);

You can see the following compass example which make uses of accelerometer and magnetic field : http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

Azimuth on Wikipédia : http://en.wikipedia.org/wiki/Azimuth

Yves M.
  • 29,855
  • 23
  • 108
  • 144
1

You have several examples of implementing a compass on Android.

jap1968
  • 7,747
  • 1
  • 30
  • 37
  • Your sample use **TYPE_ORIENTATION** which is **deprecated** – Yves M. Apr 14 '14 at 13:10
  • 1
    Take into account that samples are from the year 2010, and the answer was published in 2011. There are lots of things that have changed in the Android ecosystem in the last three years. – jap1968 Apr 14 '14 at 20:06
  • 1
    Yes sure, and don't take it personal. It's just that it's now deprecated. I want to add usefull informations for future readers :) – Yves M. Apr 15 '14 at 07:40