Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

Lesson 1: Basic positioning CHAPTER 14 545


■■maximumAge pecifies that a cached location is acceptable as long as it’s no older S
that the specified milliseconds. Default is 0, which means that a cached location is not
used.
The following is an example of passing the options to the getCurrentPosition method.
function getLocation() {
if (supportsGeolocation()) {
var options = {
enableHighAccuracy: true,
timeout: 3000,
maximumAge: 20000
};
navigator.geolocation.getCurrentPosition(showPosition, showError, options);
}
else {
showMessage("Geolocation is not supported by this browser.");
}
}

Quick check
■■What must you do to prevent sites from retrieving your location without your
knowledge?

Quick check answer
■■When you visit a site that requests your location, a pop-up is displayed, and you
must grant permission explicitly to the site before your location can be retrieved.
Be careful about selecting Always Allow because it’s easy to forget that you
selected it.

Lesson summary


■■The Geolocation API provides an interface with device location information.
■■Depending on the location source, you can receive latitude, longitude, altitude,
heading, speed, and accuracy information.
■■The Geolocation API is accessible by using the geolocation property on the navigator
global variable.
■■The Geolocation object has a getCurrentPosition method that is a one-time call to get
the current location.
■■The getCurrentPosition method takes a success callback, an error callback, and an
options parameter.
Free download pdf