有没有办法在Android浏览器上使用Javascript访问加速度计数据?我知道它支持"onorientationchange",但是我想要得到所有的东西。
澄清:我问的是如何在网站中做到这一点,而不是本机应用程序。
发布于 2012-06-04 14:58:38
从ICSAndroid4.0开始,您可以通过JavaScript事件侦听器使用'devicemotion‘事件来访问加速度计数据。有关如何访问它的信息,请参阅W3C文档- http://dev.w3.org/geo/api/spec-source-orientation.html。
注意-- W3C文档的标题命名为'device orientation',但规范中确实包含了'devicemotion‘事件文档。
发布于 2013-04-28 06:56:44
对此线程进行更新。
HTML5让别人来做这件事。检测是否存在加速度计很容易。
if (window.DeviceMotionEvent == undefined) {
//No accelerometer is present. Use buttons.
alert("no accelerometer");
}
else {
alert("accelerometer found");
window.addEventListener("devicemotion", accelerometerUpdate, true);
}
在您定义的接收加速度计事件的函数中,您可以查看accelerationIncludingGravity
成员。
function accelerometerUpdate(e) {
var aX = event.accelerationIncludingGravity.x*1;
var aY = event.accelerationIncludingGravity.y*1;
var aZ = event.accelerationIncludingGravity.z*1;
//The following two lines are just to calculate a
// tilt. Not really needed.
xPosition = Math.atan2(aY, aZ);
yPosition = Math.atan2(aX, aZ);
}
更多信息可在此处找到:http://dev.w3.org/geo/api/spec-source-orientation.html
发布于 2010-12-18 04:54:43
您可以尝试使用PhoneGap,它提供了从javascript访问加速度计的API。
对文档执行Here操作。
https://stackoverflow.com/questions/4474508
复制相似问题