谁能告诉我是否可以通过Phonegap获得以度为单位的 Android设备方向(手机相对于地面的角度)?此外,是否有可能获得磁场? PhoneGap Docs The compass is a sensor that detects the direction or heading that t
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);
如果要访问手机的x,y或z轴的旋转度,您只需使用裸HTML5即可.这是一篇很棒的文章:THIS END UP: USING DEVICE ORIENTATION
代码示例:
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);
