Geolocation — obtain the user’s latitude and longitude


The Geolocation object defines methods for determining the user’s precise geographical location. In browsers that support it, the Geolocation object is available through the Navigator object as navigator.geolocation. The methods described here depend on a few other types: locations are reported in the form of a Geoposition object and errors are reported as GeolocationError objects.


void clearWatch(long watchId)

Stops watching the user’s location. The watchId argument must be the value returned by the corresponding call to watchPosition().

void getCurrentPosition(function success, [function error], [object options])

Asynchronously determines the user’s location using any options (see the list of option properties below) that were specified. This method returns immediately, and when the user’s location becomes available, it passes a Geoposition object to the specified success callback. Or, if an error occurs (perhaps because the user did not grant permission to share her location), it passes a GeolocationError object to the error callback if one was specified.

long watchPosition(function success, [function error], [object options])

This method is like getCurrentPosition(), but after determining the user’s current location, it continues to monitor the user’s location and invokes success callback every time the position is found to have changed significantly. The return value is a number that you can pass to clearWatch() ...

Get JavaScript: The Definitive Guide, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.