accelerometer.getCurrentAcceleration

獲取當前加速沿xyz軸。

navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);

說明

加速度計是動作感應器檢測到的更改 (三角洲) 在相對於當前的設備方向,在三個維度沿xyz軸運動。

這些加速度值將返回到 [accelerometerSuccess](parameters/accelerometerSuccess.html) 回呼函數。

支援的平臺

  • Android 系統
  • 黑莓手機 WebWorks (OS 5.0 和更高)
  • iOS
  • Tizen
  • Windows Phone 7 和 8
  • Windows 8

快速的示例

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
};

function onError() {
    alert('onError!');
};

navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

完整的示例

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

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

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

    // device APIs are available
    //
    function onDeviceReady() {
        navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
        alert('Acceleration X: ' + acceleration.x + '\n' +
              'Acceleration Y: ' + acceleration.y + '\n' +
              'Acceleration Z: ' + acceleration.z + '\n' +
              'Timestamp: '      + acceleration.timestamp + '\n');
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>getCurrentAcceleration</p>
  </body>
</html>

iOS 的怪癖

  • iOS 不會認識到在任何給定的點獲取當前加速度的概念。

  • 你必須看加速和捕獲的資料在特定的時間間隔。

  • 因此, getCurrentAcceleration 收益率從報告的最後一個值的函數 watchAccelerometer 調用。