The HTML Geolocation API is used to obtain a user’s geographical position. However, due to privacy concerns, the position is only accessible if the user grants permission.
Note: Geolocation is most accurate on devices with GPS, such as smartphones. |
The Geolocation API is supported by all major browsers.
Note: The Geolocation API only works in secure contexts like HTTPS. If your site is hosted on an insecure origin (e.g., HTTP), location requests will not work. |
The getCurrentPosition() method is used to retrieve the user’s current location.
The following example returns the latitude and longitude of the user’s position:
<script> const x = document.getElementById(“demo”); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { x.innerHTML = “Geolocation is not supported by this browser.”; } } function showPosition(position) { |
Explanation of the example:
This example is a simple Geolocation script without error handling.
The second parameter of the getCurrentPosition()
method is used to handle errors. It defines a function that will be executed if the attempt to retrieve the user’s location fails.
function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML = “User denied the request for Geolocation.” break; case error.POSITION_UNAVAILABLE: x.innerHTML = “Location information is unavailable.” break; case error.TIMEOUT: x.innerHTML = “The request to get user location timed out.” break; case error.UNKNOWN_ERROR: x.innerHTML = “An unknown error occurred.” break; } } |
To display the result on a map, you need to use a map service, such as Google Maps.
In the following example, the returned latitude and longitude are used to display the location on a Google Map through a static image.
function showPosition(position) { let latlon = position.coords.latitude + “,” + position.coords.longitude; let img_url = “https://maps.googleapis.com/maps/api/staticmap?center= “+latlon+“&zoom=14&size=400×300&sensor=false&key=YOUR_KEY”; document.getElementById(“mapholder”).innerHTML = “<img src='”+img_url+“‘>”; } |
This page has shown how to display a user’s location on a map.
Geolocation is also valuable for providing location-based services such as:
The getCurrentPosition() method returns an object upon success, always including the latitude, longitude, and accuracy properties. Additional properties are returned if available.
Property |
Returns |
coords.latitude |
The latitude as a decimal number (always provided). |
coords.longitude |
The latitude as a decimal number (always provided). |
coords.accuracy |
The accuracy of the position (always provided). |
coords.altitude |
The altitude in meters above sea level (returned if available). |
coords.altitudeAccuracy |
The accuracy of the altitude position (returned if available). |
coords.heading |
The heading in degrees clockwise from North (returned if available). |
coords.speed |
The speed in meters per second (returned when available). |
timestamp |
The date and time of the response (returned if available). |
The Geolocation object also provides additional useful methods:
The example below demonstrates the watchPosition() method. To test this, an accurate GPS device (such as a smartphone) is required.
<script> const x = document.getElementById(“demo”); function getLocation() { if (navigator.geolocation) { navigator.geolocation.watchPosition(showPosition); } else { x.innerHTML = “Geolocation is not supported by this browser.”; } } function showPosition(position) { x.innerHTML = “Latitude: “ + position.coords.latitude + “<br>Longitude: “ + position.coords.longitude; } </script> |