Geolocation API
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
TheGeolocation 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 theGeolocation 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.
In this article
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 tonavigator.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 different 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 object which provides options for retrieval of the position data.
For further information on Geolocation usage, readUsing the Geolocation API.
Interfaces
GeolocationThe 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.
GeolocationPositionRepresents the position of a user. A
GeolocationPositioninstance is returned by a successful call to one of the methods contained insideGeolocation, inside a success callback, and contains a timestamp plus aGeolocationCoordinatesobject instance.GeolocationCoordinatesRepresents the coordinates of a user's position; a
GeolocationCoordinatesinstance contains latitude, longitude, and other important related information.GeolocationPositionErrorA
GeolocationPositionErroris returned by an unsuccessful call to one of the methods contained insideGeolocation, inside an error callback, and contains an error code and message.
Extensions to other interfaces
Navigator.geolocationThe entry point into the API. Returns a
Geolocationobject instance, from which all other functionality can be accessed.
Security considerations
The Geolocation API allows users to programmatically access location information insecure contexts.
Access may further be controlled by thePermissions Policy directivegeolocation.The default allowlist forgeolocation isself, which allows access to location information in same-origin nested frames only.Third party usage is enabled by setting aPermissions-Policy response header to grant permission to a particular third party origin:
Permissions-Policy: geolocation=(self b.example.com)Theallow="geolocation" attribute must then be added to the iframe element with sources from that origin:
<iframe src="https://b.example.com" allow="geolocation"></iframe>Geolocation data may reveal information that the device owner does not want to share.Therefore, users must grant explicit permission via a prompt when eitherGeolocation.getCurrentPosition() orGeolocation.watchPosition() is called (unless the permission state is alreadygranted ordenied).The lifetime of a granted permission depends on the user agent, and may be time based, session based, or even permanent.ThePermissions APIgeolocation permission can be used to test whether access to use location information isgranted,denied orprompt (requires user acknowledgement of a prompt).
Examples
SeeUsing the Geolocation API for example code.
Specifications
| Specification |
|---|
| Geolocation> # geolocation_interface> |
Browser compatibility
Availability
As Wi-Fi-based locating is often provided by Google, the vanilla Geolocation API may be unavailable in China. You may use local third-party providers such asBaidu,Autonavi, orTencent. These services use the user's IP address and/or a local app to provide enhanced positioning.
See also
- Using the Geolocation API
- Who moved my geolocation? (Hacks blog)