Search code examples
javascriptreact-nativeexpoazimuth

What the "rotation" in expo-sensors' DeviceMotion returns exactly?


I'm building an App where I need to rely on compass azimuth/bearing/heading whatever you call it: the angle between the North and the Y-axis of the phone.

I read the greater half of the Internet and learned about "Sensor fusion" and it's virtual form of ROTATION_VECTOR in Android. I got familiarized with SensorManager and DeviceMotionModule.

Boiling it down to expo-sensors' DeviceMotion, my test code looks like so:

import { DeviceMotion } from 'expo-sensors'

DeviceMotion.addListener( ({ rotation }) => 
  console.info( ( 360 + this.toDegreesInt( rotation.alpha ) ) % 360 ) 
)

Now if I rotate the device, the value changes somehow, but if I point it to the North, I get an angle of 124 degrees which is far beyond the uncalibrated compass reading.

Also, if I rotate the device 360 degrees, the supposed azimuth value never goes from 0 to 359, but rather in some randomly spread chunks.

Google Navigation on the very same device works like charm...

How can I make my compass work?


Solution

  • I ended up using the expo-location module like so:

    import * as Location from 'expo-location'
    
    Location.watchPositionAsync( {..}, ({ coords }) => console.info( coords.latitude, coords.longitude ) )
    

    The coords object has final data with valid measures, and no additional processing is required.