【发布时间】:2012-12-18 14:18:57
【问题描述】:
谁能告诉我是否有可能通过 Phonegap 获得以度为单位的 Android 设备方向(手机相对于地面的角度)?还有,是不是也可以得到磁场?
【问题讨论】:
谁能告诉我是否有可能通过 Phonegap 获得以度为单位的 Android 设备方向(手机相对于地面的角度)?还有,是不是也可以得到磁场?
【问题讨论】:
指南针是一种传感器,可检测设备指向的方向或航向。它测量从 0 到 359.99 度数的航向。
指南针航向信息通过 CompassHeading 返回 使用 compassSuccess 回调函数的对象。
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);
【讨论】:
【讨论】:
据我所知,在 phonegap android 中您可以获得accelerometer 数据
和 compass数据
【讨论】: