HTML and CSS Reference
In-Depth Information
Confi guring the geolocation
Finally, the third argument to both
getCurrentPosition
and
watchPosition
are the geolocation options. All the geoloca-
tion options are optional, as you've seen, and are made up
as follows:
•
enableHighAccuracy
(Boolean, default false)
•
timeout
(in milliseconds, default infi nity/0)
•
maximumAge
(in milliseconds, default 0)
For example, to request high accuracy, a two-second timeout,
and to never use old geo data, call
getCurrentPosition
using
the following options (where
success
and
error
are predefi ned
functions):
navigator.geolocation.getCurrentPosition(success, error, {
enableHighAccuracy: true,
timeout: 2000,
maximumAge: 0
});
The
enableHighAccuracy
tells the device to try to get a more
accurate reading on the latitude and longitude. On a mobile
device, this may be to make use of the GPS on a phone, which
could consume more power on the mobile device. Typically, you
want the battery to last as long as possible, which is why by
default,
enableHighAccuracy
is set to false.
The timeout tells the geolocation lookup how long it should
wait before giving up and triggering the error handler (but won't
start counting down if it's waiting for the user to approve the
request). If it does timeout, the error code is set to 3 (
TIMEOUT
).
Setting a zero time out (the current default) tells the browser to
never time out.
Finally,
maximumAge
can be used to tell the browser whether to
use recently cached position data. If there is a request that is
within the
maximumAge
(in milliseconds), it is returned instead of
requesting a new position.
maximumAge
can also be
Infinity
,
which tells the browser to always use a cached position. Setting
the
maximumAge
to zero (the default value) means the browser
must look up a new position on each request.