--- title: Geolocation.watchPosition() slug: Web/API/Geolocation/watchPosition tags: - API - Exemple - Géolocalisation - Géolocalisation API - Méthode - Reference translation_of: Web/API/Geolocation/watchPosition ---
{{ APIref("Geolocation API") }}
La méthode Geolocation.watchPosition()
permet de manipuler une fonction appelée automatiquement à chaque fois que la position de l'appareil change. Vous pouvez de même, en option, manipuler une autre fonction appelée automatiquement pour gérer les erreurs.
Cette méthode retourne une valeur ID de veille qui permet de libérer les fonctions déclarées automatiquement, évoquées précédemment, à l'aide de la méthode {{domxref("Geolocation.clearWatch()")}}.
id = navigator.geolocation.watchPosition(success[, error[, options]])
var id, target, options; function success(pos) { var crd = pos.coords; if (target.latitude === crd.latitude && target.longitude === crd.longitude) { console.log('Bravo, vous avez atteint la cible'); navigator.geolocation.clearWatch(id); } } function error(err) { console.warn('ERROR(' + err.code + '): ' + err.message); } target = { latitude : 0, longitude: 0 }; options = { enableHighAccuracy: false, timeout: 5000, maximumAge: 0 }; id = navigator.geolocation.watchPosition(success, error, options);
Spécification | Statut | Commentaire |
---|---|---|
{{SpecName('Geolocation', '#watch-position', 'Geolocation.watchPosition()')}} | {{Spec2('Geolocation')}} | Specification initiale. |
{{ CompatibilityTable() }}
Caractère | Chrome | Limite | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 5 | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("1.9.1")}} | 9 | 10.60 Supprimé de 15.0 Réintroduit en 16.0 |
5 |
Caractère | Android | Chrome pour Android | Limite | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown()}} | {{CompatUnknown()}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("4")}} | {{CompatUnknown()}} | 10.60 | {{CompatUnknown()}} |