Can anyone tell me whether it is possible though Phonegap to get Android Device Orientation in degrees (angle of the phone relative to the ground)? Also, is it possible to get the Magnetic Field as well?
Asked
Active
Viewed 9,457 times
3 Answers
13
The compass is a sensor that detects the direction or heading that the device is pointed. It measures the heading in degrees from 0 to 359.99.
The compass heading information is returned via a CompassHeading object using the compassSuccess callback function.
function onSuccess(heading) {
alert('Heading: ' + heading.magneticHeading);
};
function onError(error) {
alert('CompassError: ' + error.code);
};
navigator.compass.getCurrentHeading(onSuccess, onError);
If you want to access the rotation-degrees of the x,y or z axis of the Phone you can simply use bare HTML5. Here is a great article about it: THIS END UP: USING DEVICE ORIENTATION
Code example:
window.addEventListener('deviceorientation', function(eventData) {
// gamma is the left-to-right tilt in degrees, where right is positive
var tiltLR = eventData.gamma;
// beta is the front-to-back tilt in degrees, where front is positive
var tiltFB = eventData.beta;
// alpha is the compass direction the device is facing in degrees
var dir = eventData.alpha
// deviceorientation does not provide this data
var motUD = null;
// call our orientation event handler
deviceOrientationHandler(tiltLR, tiltFB, dir, motUD);
}, false);

Stephan Bönnemann-Walenta
- 3,383
- 1
- 22
- 35
-
Hi Stephan, thanks for your reply - guess I did not make my question clear. I am aware about the compass, however what I need is the angle that the mobile is tilted relative to the ground. Can this be determined by the compass as well? – user1809790 Dec 18 '12 at 14:46
-
Excellent! Also, is there a way to lock the axis in phonegap? The reason is because if the user places his mobile phone flat, the z-axis would be pointed upwards. However, if the user puts the mobile phone in a car phone holder, the z-axis would most probably be pointing towards and away from you. Not sure how to go around this one :( – user1809790 Dec 18 '12 at 14:57
-
@user1809790 I think not. I once had the same problem, and couldn't find an easy way to fix this issue. You probably have to normalize the data received on pageload and offer the user a way to calibrate the orientation. – Stephan Bönnemann-Walenta Dec 18 '12 at 15:15
-
Do you have any tutorial by any chance on how to do the normalization? I am new to all this and am trying to create an app using Phonegap & JQuery Mobile – user1809790 Dec 18 '12 at 15:26
-
@user1809790 Normalization simply means that you don't use the absolute values reported, but you have a reference orientation recorded (e.g. on App start or the calibration button I mentioned) and use all values relative to these values. And no unfortunately I don't have a tutorial at hand :/ – Stephan Bönnemann-Walenta Dec 18 '12 at 15:29
0
As far as i know, in phonegap android you can get the accelerometer data
and compass data
-
Is the accelerometer similar to a gyroscope? And how accurate is it? – user1809790 Dec 18 '12 at 14:47