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 Geolocation.clearWatch()
.
Syntaxe
id = navigator.geolocation.watchPosition(success[, error[, options]])
Paramètres
- success
- Nom d'une fonction appelée qui a pour paramètre l'objet
Position
. - error Facultatif
- Nom d'une fonction optionnelle qui a pour paramètre l'objet
PositionError
. - options Facultatif
- Un objet optionnel
PositionOptions
.
Exemple
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);
Note : Si votre application fonctionne sous Firefox OS, veillez à la geolocation wake lock pour que votre application continue à recevoir les changements de positions si votre application tourne en tâche de fond, ou si votre écran est éteint.
Spécifications
Spécification | Statut | Commentaire |
---|---|---|
Geolocation API La définition de 'Geolocation.watchPosition()' dans cette spécification. |
Recommendation | Specification initiale. |
Navigateurs compatibles
Nous convertissons les données de compatibilité dans un format JSON.
Ce tableau de compatibilité utilise encore l'ancien format
car nous n'avons pas encore converti les données qu'il contient.
Vous pouvez nous aider en contribuant !
Caractère | Chrome | Limite | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 5 | (Oui) | 3.5 (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 | ? | ? | (Oui) | 4.0 (4) | ? | 10.60 | ? |
Voir également
- geolocation wake lock
- Using geolocation
- L'interface qui lui est réservée,
Geolocation
, et la façon d'y accéderNavigatorGeolocation.geolocation
. - L'opération inverse :
Geolocation.clearWatch()
- Une méthode similaire :
Geolocation.getCurrentPosition()