540 CHAPTER 14 Making your HTML location-aware
Before you begin
To complete this book, you must have some understanding of web development. This chapter
requires the hardware and software listed in the “System requirements” section in the book’s
Introduction.
Lesson 1: Basic positioning
The Geolocation object is accessible by using the navigator.geolocation global variable. Most
browsers support the Geolocation API, and the following code can be used to determine
whether the user’s browser provides support.
function supportsGeolocation() {
return 'geolocation' in navigator;
}
This code checks whether a geolocation property is on the object that’s referenced by the
navigator global variable. If the property exists, it will reference a Geolocation object.
After this lesson, you will be able to:
■■Understand the Geolocation object.
■■Retrieve the current position.
■■Handle positioning errors.
Estimated lesson time: 20 minutes
Geolocation object reference
The Geolocation object has a simple API with the following methods.
■■getCurrentPosition() Method that accepts a success callback, an error callback, and
options array and calls the success callback with the current position
■■watchPosition() Method that accepts a success callback, an error callback, and
options array and continuously calls the success callback with the current position
■■clearWatch() Method that stops continuous calling that was started by using the
watchPosition method
Notice that the getCurrentPosition and watchPosition methods have the same parameters.
The Position object has the following properties.
■■coords Gets a Coordinates object that contains information about the current
position.