HTML and CSS Reference
In-Depth Information
eXerCISe 12-2. DISpLaYING the LOCatION
1.
Add the script element shown in listing 12-1 to the end of the body element.
Listing 12-1. Displaying the location
<script type = "text/javascript">
var lbl = document.getElementById("lbl");
if (navigator.geolocation) {
navigator.geolocation
.getCurrentPosition(showLocation,
errorHandler,
{
maximumAge: 100,
timeout: 6000,
enableHighAccuracy: true
});
}
else {
alert("Geolocation not suported");
}
function showLocation(pos) {
lbl.innerHTML =
"Your latitude: " + pos.coords.latitude +
" and longitude: " + pos.coords.longitude +
" (Accuracy of: " + pos.coords.accuracy + " meters)";
}
function errorHandler(e) {
if (e.code === 1) { // PERMISSION_DENIED
lbl.innerHTML = "Permission denied. - " + e.message;
} else if (e.code === 2) { //POSITION_UNAVAILABLE
lbl.innerHTML = "Make sure your network connection is active and " +
"try this again. - " + e.message;
} else if (e.code === 3) { //TIMEOUT
lbl.innerHTML = "A timeout ocurred; try again. - " + e.message;
}
}
</script>
 
Search WWH ::




Custom Search