HTML and CSS Reference
In-Depth Information
calling the clearWatch method. You could call this method in either the success or the error
callback—for example, to cancel polling when you have captured enough position informa-
tion or when you want to pause polling for a period of time:
geoLocator.clearWatch(watcher);
Listing 1-6 shows the full solution code for the watchPosition example.
LISTING 1-6 Using the Geolocation API to monitor position
var watcher;
var geoLocator;
window.onload = function () {
geoLocator = window.navigator.geolocation;
var posOptions = {enableHighAccuracy: true,timeout: 45000};
watcher = geoLocator.watchPosition(successPosition, errorPosition, posOptions);
}
function successPosition(pos) {
var sp = document.createElement("p");
sp.innerText = "Latitude: " + pos.coords.latitude + " Longitude: "
+ pos.coords.longitude;
document.getElementById("geoResults").appendChild(sp);
geoLocator.clearWatch(watcher);
}
function errorPosition(err) {
var sp = document.createElement("p");
sp.innerText = "error: " + err.message; + " code: " + err.code;
document.getElementById("geoResults").appendChild(sp);
}
Figure 1-55 shows the output of this code on a mobile device.
FIGURE 1-55 Multiple positions being recorded by the watchPosition method
 
Search WWH ::




Custom Search