当前位置 : 主页 > 手机开发 > cordova >

cordova – Phonegap – 以度和磁场为单位的设备方向

来源:互联网 收集:自由互联 发布时间:2021-06-10
谁能告诉我是否可以通过Phonegap获得以度为单位的 Android设备方向(手机相对于地面的角度)?此外,是否有可能获得磁场? PhoneGap Docs The compass is a sensor that detects the direction or heading that t
谁能告诉我是否可以通过Phonegap获得以度为单位的 Android设备方向(手机相对于地面的角度)?此外,是否有可能获得磁场? PhoneGap Docs

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);
网友评论