The Geolocation API allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information.
WebExtensions that wish to use the Geolocation object must add the
"geolocation" permission to their manifest. The user's operating system will prompt the user to allow location access the first time it is requested.Concepts and usage
You will often want to retrieve a user's location information in your web app, for example to plot their location on a map, or display personalized information relevant to their location.
The Geolocation API is accessed via a call to
Navigator.geolocation; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information (for example, GPS).The developer can now access this location information in a couple of diferent ways:
Geolocation.getCurrentPosition(): Retrieves the device's current location.Geolocation.watchPosition(): Registers a handler function that will be called automatically each time the position of the device changes, returning the updated location.
In both cases, the method call takes up to three arguments:
- A mandatory success callback: If the location retrieval is successful, the callback executes with a
GeolocationPositionobject as its only parameter, providing access to the location data. - An optional error callback: If the location retrieval is unsuccessful, the callback executes with a
GeolocationPositionErrorobject as its only parameter, providing access information on what went wrong. - An optional
PositionOptionsobject, which provides options for retrieval of the position data.
For further information on Geolocation usage, read Using the Geolocation API.
Interfaces
Geolocation- The main class of this API — contains methods to retrieve the user's current position, watch for changes in their position, and clear a previously-set watch.
GeolocationPosition- Represents the position of a user. A
GeolocationPositioninstance is returned by a successful call to one of the methods contained insideGeolocation, inside a sucess callback, and contains a timestamp plus aGeolocationCoordinatesobject instance. GeolocationCoordinates- Represents the coordinates of a user's position; a
GeolocationCoordinatesinstance contains latitude, longitude, and other important related information. GeolocationPositionError- A
GeolocationPositionErroris returned by an unsuccessful call to one of the methods contained insideGeolocation, inside an error callback, and contains an error code and message. Navigator.geolocation- The entry point into the API. Returns a
Geolocationobject instance, from which all other functionality can be accessed.
Dictionaries
PositionOptions- Represents an object containing options to pass in as a parameter of
Geolocation.getCurrentPosition()andGeolocation.watchPosition().
Examples
In the following example the Geolocation API is used to retrieve the user's latitude and longitude. If sucessful, the available hyperlink is populated with an
openstreetmap.org URL that will show their location.HTML Content
<button id = "find-me">Show my location</button><br/>
<p id = "status"></p>
<a id = "map-link" target="_blank"></a>
JavaScript Content
function geoFindMe() {
const status = document.querySelector('#status');
const mapLink = document.querySelector('#map-link');
mapLink.href = '';
mapLink.textContent = '';
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
status.textContent = '';
mapLink.href = `https://www.openstreetmap.org/#map=18/${latitude}/${longitude}`;
mapLink.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`;
}
function error() {
status.textContent = 'Unable to retrieve your location';
}
if (!navigator.geolocation) {
status.textContent = 'Geolocation is not supported by your browser';
} else {
status.textContent = 'Locating…';
navigator.geolocation.getCurrentPosition(success, error);
}
}
document.querySelector('#find-me').addEventListener('click', geoFindMe);
Live Result
Specifications
| Specification | Status | Comment |
|---|---|---|
| Geolocation API | RECRecommendation |
Browser compatibility
Update compatibility data on GitHub
| 5 | 12 |
3.5 | 9 | 16 | 5 | Yes | Yes | 4 | 16 | Yes | Yes |
| 5 | 12 | 3.5 | 9 | 16 | Yes | Yes | Yes | 4 | 16 | Yes | Yes |
| 5 | 12 | 3.5 | 9 | 16 | Yes | Yes | Yes | 4 | 16 | Yes | Yes |
| 50 | ? | 55 | No | 37 | Yes |
51 | 50 | 55 | 37 | Yes | ? |
| 5 | 12 | 3.5 | 9 | 16 | Yes | Yes | 18 | 4 | 16 | Yes | Yes |
Legend
- Full support
- No support
- Compatibility unknown
- See implementation notes.
Availability
As WiFi-based locationing is often provided by Google, the vanilla Geolocation API may be unavailable in China. You may use local third-party providers such as Baidu, Autonavi, or Tencent. These services use the user's IP address and/or a local app to provide enhanced positioning.
