Skip to main content

HTML Geolocation API

The Geolocation API is used to get the user's current location.


Locate the User's Position

The Geolocation API gets the user's current location. Because this can affect privacy, the location is not available unless the user approves it.

Note: The Geolocation API only works on secure contexts such as HTTPS. Tip: It is most accurate on devices with GPS, like smartphones or smartwatches.


Browser Support

Geolocation is supported in all modern browsers (from Chrome 5, Edge 12, Firefox 3.5, Safari 5, and Opera 10.6).


Using HTML Geolocation API

The Geolocation API is reached through navigator.geolocation, which makes the browser ask the user for permission to access their location. If the user accepts, the browser uses the best feature on the device (such as GPS). The getCurrentPosition() method returns the user's current location.

The example checks whether geolocation is supported and, if so, runs getCurrentPosition(); otherwise it shows a message. The success function outputs the latitude and longitude, and the error function alerts a message if it fails.

Example

Showing the user's latitude and longitude.

Prompt
A page with a button that, when clicked, uses the Geolocation API getCurrentPosition to show the user's latitude and longitude in a paragraph, with a fallback message if geolocation is not supported and an alert on error.
Output: complete HTML page

Error Handling and Rejections

The second parameter of getCurrentPosition() handles errors; it names a function to run if getting the location fails. You can inspect error.code for a specific message, handling cases like PERMISSION_DENIED, POSITION_UNAVAILABLE, TIMEOUT, and UNKNOWN_ERROR.

Example

Geolocation with detailed error messages.

Prompt
A page like the basic geolocation demo but with detailed error handling: the error callback inspects error.code and shows a specific message for PERMISSION_DENIED, POSITION_UNAVAILABLE, TIMEOUT, and UNKNOWN_ERROR.
Output: complete HTML page

Location-specific Information

Geolocation is useful for location-specific information, such as up-to-date local information, showing points of interest near the user, and turn-by-turn GPS navigation.


The getCurrentPosition() Method - Return Data

On success, getCurrentPosition() returns an object. It always returns coords.latitude, coords.longitude, and coords.accuracy. The others are returned only if available: coords.altitude (meters above sea level), coords.altitudeAccuracy, coords.heading (degrees clockwise from north), coords.speed (meters per second), and timestamp (the date and time of the response).


Geolocation Object - Other interesting Methods

The Geolocation object also has other methods: watchPosition() returns the current location and keeps returning updates as the user moves (like the GPS in a car), and clearWatch() stops watchPosition(). You need an accurate GPS device, like a smartphone, to test watchPosition().

Example

Continuously tracking the user's location with watchPosition().

Prompt
A page that uses the Geolocation watchPosition method to continuously show the user's updated latitude and longitude as they move, with the same detailed error handling (best tested on a GPS device).
Output: complete HTML page