geolocation.clearWatch

再看對所引用的設備的位置更改為 watchID 參數。

navigator.geolocation.clearWatch(watchID);

參數

  • watchID: 的 id watchPosition 清除的時間間隔。(字串)

說明

geolocation.clearWatch將停止觀看對設備的位置的更改通過清除 [geolocation.watchPosition](geolocation.watchPosition.html) 引用的watchID.

支援的平臺

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

快速的示例

/ / 選項: 監視的更改的位置,並使用最 / / 準確定位採集方法可用。
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });

// ...later on...

navigator.geolocation.clearWatch(watchID);

完整的示例

<!DOCTYPE html>
<html>
  <head>
    <title>Device Properties 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);

    var watchID = null;

    // device APIs are available
    //
    function onDeviceReady() {
        // Get the most accurate position updates available on the
        // device.
        var options = { enableHighAccuracy: true };
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
    }

    // onSuccess Geolocation
    //
    function onSuccess(position) {
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                            'Longitude: ' + position.coords.longitude     + '<br />' +
                            '<hr />'      + element.innerHTML;
    }

    // clear the watch that was started earlier
    //
    function clearWatch() {
        if (watchID != null) {
            navigator.geolocation.clearWatch(watchID);
            watchID = null;
        }
    }

        // onError Callback receives a PositionError object
        //
        function onError(error) {
          alert('code: '    + error.code    + '\n' +
                'message: ' + error.message + '\n');
        }

    </script>
  </head>
  <body>
    <p id="geolocation">Watching geolocation...</p>
        <button onclick="clearWatch();">Clear Watch</button>
  </body>
</html>