geolocation.watchPosition
Observer les changements de localisation de l'appareil.
var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
Paramètres
geolocationSuccess : la fonction callback à laquelle la position actuelle de l'appareil est transmise.
geolocationError : (facultative) la fonction callback s'exécutant lorsqu'une erreur survient.
geolocationOptions : (facultatives) options de personnalisation de la géolocalisation.
Retourne
- String : retourne un identifiant faisant référence à l'intervalle d'observation de la position. Cet identifiant est prévu pour être utilisé avec
[geolocation.clearWatch](geolocation.clearWatch.html)
afin de pouvoir arrêter ultérieurement l'observation des changements de position.
Description
geolocation.watchPosition
est une fonction asynchrone. Elle renvoie la position actuelle de l'appareil lorsqu'un changement de position est détecté. Lorsque l'appareil récupère un nouvelle position, la fonction callback [geolocationSuccess](parameters/geolocationSuccess.html)
est exécutée avec un objet [Position](Position/position.html)
comme paramètre. Si une erreur se produit, la fonction callback [geolocationError](parameters/geolocationError.html)
est appelée avec un obet [PositionError](PositionError/positionError.html)
comme paramètre.
Plates-formes supportées
- Android
- BlackBerry WebWorks (OS 5.0 et plus)
- iOS
- Paciarelli
- Windows Phone 7 et 8
- Windows 8
Exemple court
// onSuccess Callback
// This method accepts a `Position` object, which contains
// the current GPS coordinates
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Options: throw an error if no update is received every 30 seconds.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });
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() {
// Throw an error if no update is received every 30 seconds
var options = { timeout: 30000 };
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;
}
// 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>
</body>
</html>