blog

Geolocation + Accelerometer = Orientation

In order to get orientation of your android device you need to do “some” math, based on geolocation and accelerometer values. Working with Android native SDK, you are able to get orientation as easy as calling SensorManager.getOrientation(), but when it comes to AIR SDK, you have no access to the native SDK methods. However, thanks to open sources you are able to grab the original source and rewrite the thing into ActionScript. That is exatcly what I have done. The results are GeomagneticField and SensorManager classes. To make this happen you need both AccelerometerEvent and GeolocationEvent values as intput:

import sk.yoz.hardware.GeomagneticField;
import sk.yoz.hardware.SensorManager;

var event1:GeolocationEvent; // real event with correct values
var event2:AccelerometerEvent; // real event with correct values
var R:Array = new Array(9);
var geomagneticField:GeomagneticField = new GeomagneticField(
    event1.latitude, event1.longitude, event1.altitude, new Date().time);
SensorManager.getRotationMatrix(
    event2.accelerationX, event2.accelerationY, event2.accelerationZ,
    geomagneticField.x, geomagneticField.y, geomagneticField.z, R);
var orientation:Array = SensorManager.getOrientation(R);
orientation[0] // orientation X in radians
orientation[1] // orientation Y in radians
orientation[2] // orientation Z in radians

… do not forget to add permissions into manifest file

<uses-permission android:name="android.permission.LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

SensorEvent

android hardware SensorManager

SensorManager.java

GeomagneticField.java