Phonegap 加速度

<!DOCTYPE html>
<html>
<head>
<title>Acceleration Example</title>

<script type="text/javascript" charset="utf-8" src="cordova-1.8.1.js"></script>
<script type="text/javascript" charset="utf-8">

// The watch id references the current `watchAcceleration`
var watchID = null;

// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {
startWatch();
}

// Start watching the acceleration
//
function startWatch() {

// Update acceleration every 1 seconds
var options = { frequency: 1000 };

watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}

// Stop watching the acceleration
//
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}

// onSuccess: Get a snapshot of the current acceleration
//
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: Failed to get the acceleration
//
function onError() {
alert('onError!');
}

</script>
</head>
<body>
<div id="accelerometer">Waiting for accelerometer...</div>
</body>
</html>
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 意見 :

張貼留言