Phonegap - 度和磁场的设备方向

use*_*790 8 phonegap-plugins cordova

谁能告诉我是否可以通过Phonegap以度数(手机相对于地面的角度)获得Android设备方向?此外,是否有可能获得磁场?

Ste*_*ann 13

PhoneGap Docs

指南针是一种传感器,可检测设备指向的方向或方向.它以0到359.99度为单位测量航向.

使用compassSuccess回调函数通过CompassHeading对象返回罗盘标题信息.

function onSuccess(heading) {
    alert('Heading: ' + heading.magneticHeading);
};

function onError(error) {
    alert('CompassError: ' + error.code);
};

navigator.compass.getCurrentHeading(onSuccess, onError);
Run Code Online (Sandbox Code Playgroud)

如果要访问手机的x,y或z轴的旋转度,您只需使用裸HTML5即可.这是一篇很棒的文章:最后:使用设备方向

代码示例:

  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);
Run Code Online (Sandbox Code Playgroud)

例