HTML and CSS Reference
In-Depth Information
Lastly, the getCurrentPosition() method can be handed a custom object that
can be used to set various options used in retrieving the location. This object can be set
up with the following properties and values:
enableHighAccuracy : Set to true or false . If enabled, the highest ac-
curacy method for determining the location is used, such as GPS. Be aware that
this will increase battery usage and the length of time for retrieving the location.
timeout : How long to wait (in milliseconds) when retrieving the location be-
fore throwing a position unavailable error.
maximumAge : How long (in milliseconds) a particular position should be con-
sidered the current position.
These options may be added to the getCurrentPosition() method using short-
hand object creation notation, like so:
var options = {
enableHighAccuracy: true,
timeout: 120000,
maximumAge: 1000
};
win-
dow.navigator.geolocation.getCurrentPosition(locatedSuccess,
locatedFailed, options);
This will enable high-accuracy positioning (which is dependent on the hardware
available), set the timeout to two minutes, and set the maximum age of the location to
one second.
Watching the current position
Getting the location once is fine for a stationary device, such as a desktop computer, but
for a mobile device, the location would have to be continually retrieved to be accurate.
The watchPosition() method is used to continually poll the location (this is where
the maximum age option is useful) to update the location information. It takes the same
arguments as the getCurrentPosition() method, but it should be set to a variable
that can later be referenced and handed to the clearWatch() method if updating the
location continuously is stopped. Here's an example:
var geoWatchID = win-
dow.navigator.geolocation.watchPosition(locatedSuccess,
locatedFailed, options);
Search WWH ::




Custom Search