gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:54:15 25 4
gpt4 key购买 nike

谁能告诉我是否有可能通过 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);

example

关于cordova - Phonegap - 以度数和磁场为单位的设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com