LevinsonHou 2013-05-02
采集设备在x、y、z方向上的动作。
accelerometer.getCurrentAcceleration:返回当前沿x、y和z方向的加速度。
navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);说明:
<!DOCTYPE html> <html> <head> <title>Acceleration Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { navigator.accelerometer.getCurrentAcceleration(onSuccess, onError); } // onSuccess: 返回当前加速度数据的快照 function onSuccess(acceleration) { alert('Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: ' + acceleration.timestamp + '\n'); } // onError: 返回加速度数据失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>getCurrentAcceleration</p> </body> </html>iPhone的特异情况:
accelerometer.watchAcceleration:在固定的时间间隔获取沿x、y和z轴的加速度。
var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess, accelerometerError, [accelerometerOptions]);说明:
<!DOCTYPE html> <html> <head> <title>Acceleration Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // watch id 是当前“watchAcceleration”的引用 var watchID = null; // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { startWatch(); } // 开始监视加速度 function startWatch() { // 每隔3秒钟更新一次加速度数据 var options = { frequency: 3000 }; watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options); } // 停止监视加速度 function stopWatch() { if (watchID) { navigator.accelerometer.clearWatch(watchID); watchID = null; } } // onSuccess: 获取当前加速度数据的快照 function onSuccess(acceleration) { var element = document.getElementById('accelerometer'); element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' + 'Acceleration Y: ' + acceleration.y + '<br />' + 'Acceleration Z: ' + acceleration.z + '<br />' + 'Timestamp: ' + acceleration.timestamp + '<br />'; } // onError: 获取加速度失败 function onError() { alert('onError!'); } </script> </head> <body> <div id="accelerometer">Waiting for accelerometer...</div> </body> </html>iPhone的特异情况:
accelerometer.clearWatch:停止watch ID参数指向的加速度监视。
navigator.accelerometer.clearWatch(watchID);watchID:由accelerometer.watchAcceleration返回的引用标识ID。
完整的范例:
<!DOCTYPE html> <html> <head> <title>Acceleration Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // watch id 是当前“watchAcceleration”的引用 var watchID = null; // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { startWatch(); } // 开始监视加速度 function startWatch() { // 每隔3秒钟更新一次加速度数据 var options = { frequency: 3000 }; watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options); } // 停止监视加速度 function stopWatch() { if (watchID) { navigator.accelerometer.clearWatch(watchID); watchID = null; } } // onSuccess: 获取当前加速度数据的快照 function onSuccess(acceleration) { var element = document.getElementById('accelerometer'); element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' + 'Acceleration Y: ' + acceleration.y + '<br />' + 'Acceleration Z: ' + acceleration.z + '<br />' + 'Timestamp: ' + acceleration.timestamp + '<br />'; } // onError: 获取加速度失败 function onError() { alert('onError!'); } </script> </head> <body> <div id="accelerometer">Waiting for accelerometer...</div> <button onclick="stopWatch();">Stop Watching</button> </body> </html>Acceleration:包含特定时间点采集到的加速计数据。
完整的范例:
<!DOCTYPE html> <html> <head> <title>Acceleration Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { navigator.accelerometer.getCurrentAcceleration(onSuccess, onError); } // onSuccess: 获得加速度数据的快照 function onSuccess(acceleration) { alert('Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: ' + acceleration.timestamp + '\n'); } // onError: 获取加速度失败 function onError() { alert('onError!'); } </script> </head> <body> <h1>Example</h1> <p>getCurrentAcceleration</p> </body> </html>accelerometerSuccess:提供加速度信息的onSuccess回调函数.
accelerometerOptions:定制检索加速度计的可选参数