geolocation.watchPosition
Orologi per le modifiche alla posizione corrente del dispositivo.
var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
Parametri
geolocationSuccess: il callback passato alla posizione corrente.
geolocationError: (facoltativo) il callback che viene eseguito se si verifica un errore.
geolocationOptions: opzioni (opzionale) la geolocalizzazione.
Restituisce
- Stringa: restituisce un id di orologio che fa riferimento l'intervallo di posizione orologio. L'id dell'orologio deve essere usato con
[geolocation.clearWatch](geolocation.clearWatch.html)
a smettere di guardare per cambiamenti di posizione.
Descrizione
geolocation.watchPosition
è una funzione asincrona. Restituisce la posizione corrente del dispositivo quando viene rilevata una modifica della posizione. Quando il dispositivo recupera una nuova posizione, la [geolocationSuccess](parameters/geolocationSuccess.html)
callback viene eseguita con un Position
oggetto come parametro. Se c'è un errore, il [geolocationError](parameters/geolocationError.html)
callback viene eseguita con un [PositionError](PositionError/positionError.html)
oggetto come parametro.
Piattaforme supportate
- Android
- BlackBerry WebWorks (OS 5.0 e superiori)
- iOS
- Tizen
- Windows Phone 7 e 8
- Windows 8
Esempio rapido
// 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 });
Esempio completo
<!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>