2012-12-18 41 views

回答

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); 

如果您要访问的旋转度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

+0

嗨斯蒂芬,感谢您的答复 - 猜我没有提出我的问题清晰。 我知道指南针,但是我需要的是移动相对于地面倾斜的角度。这也可以由指南针来确定吗? – user1809790

+0

@ user1809790我已分别更新了帖子。 –

+0

优秀!另外,有没有办法在手机中锁定轴?原因是因为如果用户将他的手机放平,则Z轴将被向上指向。但是,如果用户将手机放在车载电话座中,则z轴很可能会指向和远离你。不知道如何去解决这个问题:( – user1809790

1

有可能使用HTML5

访问 here更多的细节,以确定设备定位。

由于我们可以使用加速度计使用html5/phonegap,我们可以从中获取定位角度和所有这些数据。