我可以在跨平台的方式中使用Javascript来获取iOS和Android(使用Chrome)的指南针标题,而无需使用像PhoneGap这样的东西吗?我知道iOS有DeviceOrientationEvent,但我在Chrome for Android上找不到任何等价物.
对于智能手机的增强现实网络应用程序,我正在尝试计算当用户手持设备时的罗盘方向,屏幕在垂直平面中,屏幕顶部朝上.
我从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;
}
}
Run Code Online (Sandbox Code Playgroud)
而.toDeg()是一个Number对象扩展,由http://www.movable-type.co.uk/scripts/latlong.html提供
/** Converts radians to numeric (signed) degrees */
if (typeof Number.prototype.toDeg == 'undefined') { …Run Code Online (Sandbox Code Playgroud)