请问通过Phonegap是否可以获取Android设备的方向(手机相对于地面的角度)?此外,是否也可以获取磁场信息?
请问通过Phonegap是否可以获取Android设备的方向(手机相对于地面的角度)?此外,是否也可以获取磁场信息?
指南针是一种感应器,用于检测设备的朝向或方向。它以0到359.99度的角度测量方向。
指南针方向信息通过 CompassSuccess 回调函数返回 CompassHeading 对象。
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);