我已经采用了http://dev.w3.org/geo/api/spec-source-orientation中建议的公式(请参见工作示例),并实现了以下函数:
function compassHeading(alpha, beta, gamma) {
var a1, a2, b1, b2;
if ( beta !== 0 || gamma !== 0 ) {
a1 = -Math.cos(alpha) * Math.sin(gamma);
a2 = Math.sin(alpha) * Math.sin(beta) * Math.cos(gamma);
b1 = -Math.sin(alpha) * Math.sin(gamma);
b2 = Math.cos(alpha) * Math.sin(beta) * Math.cos(gamma);
return Math.atan((a1 - a2) / (b1 + b2)).toDeg();
}
else {
return 0;
}
}
.toDeg()是一个Number对象扩展,由http://www.movable-type.co.uk/scripts/latlong.html提供。
/** Converts radians to numeric (signed) degrees */
if (typeof Number.prototype.toDeg == 'undefined') {
Number.prototype.toDeg = function() {
return this * 180 / Math.PI;
};
}
然而问题在于,即使设备(Google Galaxy Nexus)处于静止状态,计算出的指南针方位值仍会从大约-75跳变到80。这似乎发生在Google Chrome BETA和FF BETA 23中。请问是否有人看到我的方法存在错误或知道更可靠的计算指南针方位的方法?
deviceorientationabsolute
事件,应该使用它来获取基于世界坐标系的坐标。有关信息和状态,请参见:https://developer.mozilla.org/en-US/docs/Web/API/Window/ondeviceorientationabsolute - richtMath.atan2
,因为Math.atan
仅提供+-pi/2
之间的角度。 - flawr