geolocation.clearWatch

Arrêter d'observer les changements de position de l'appareil référencés par le paramètre watchID.

navigator.geolocation.clearWatch(watchID);

Paramètres

  • watchID : l'identifiant de l'intervalle watchPosition à effacer. (String)

Description

La méthode geolocation.clearWatch permet de cesser d'observer les modifications apportées à l'emplacement de l'appareil en désactivant l'observateur associé [geolocation.watchPosition](geolocation.watchPosition.html) identifié par le watchID donné.

Plates-formes supportées

  • Android
  • BlackBerry WebWorks (OS 5.0 et plus)
  • iOS
  • Paciarelli
  • Windows Phone 7 et 8
  • Windows 8

Exemple court

// Options : observer les changements de position, et utiliser
// la méthode d'acquisition la plus précise disponible.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });

// ...later on...

navigator.geolocation.clearWatch(watchID);

Exemple complet

<!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>