Tutorials
THE WORLD'S LARGEST WEB DEVELOPER SITE

HTML5 Geolocation

The HTML Geolocation API is used for locating the position of a user.

Locate the User's Position

 

The HTML Geolocation API is used for locating the position of a user.

As this can be vulnerable to privacy, the position cannot be madet available unless and until the user approves it.

Note: Geolocation is very accurate for electronic gadgets with GPS, like iPhone.

Browser Support

 

The table numbers specifies the first browser version which fully supports Geolocation.

API
Geolocation 5.0 - 49.0 (http)
50.0 (https)
9.0 3.5 5.0 16.0

Note: As of Chrome 50, the Geolocation API will be only working on secure contexts such as HTTPS. If your site is getting hosted on an non-secure origin (such as HTTP) the requests for getting the users location will not function.

Using HTML Geolocation

 

The getCurrentPosition() method is used for getting the user's position.

The example below illustrates how it returns user's position in terms of lattitude and longitude:

Example

<script>
var 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) {
    x.innerHTML = "Latitude: " + position.coords.latitude +
    "<br>Longitude: " + position.coords.longitude;
}
</script>
Try it Yourself

Example explained:

The example stated above can be said to be a very basic Geolocation script, without any error handling.

Handling Errors and Rejections

 

The second parameter of the getCurrentPosition() method is used for handling errors. It instructs a function to run incase if there is a failure to get the user's location:

Example

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;
    }
}
Try it Yourself

Location-specific Information

 

This page is used for demonstration purpose on how to show a user's position on a map.

Geolocation is also highly useful for getting location-specific information, like:

The getCurrentPosition() Method - Return Data


 

The getCurrentPosition() method will return an object if it is successful. The properties such as latitude, longitude and accuracy are returned. The other properties are also returned if they are available:

Property Returns
coords.latitude The latitude is returned as a decimal number (always returned)
coords.longitude The longitude is returned as a decimal number (always returned)
coords.accuracy The position accuracy (always returned)
coords.altitude The altitude is returned in meters above the mean sea level (returned if available)
coords.altitudeAccuracy The position altitude accuracy (returned if available)
coords.heading The heading measured in degrees clockwise from North (returned if available)
coords.speed The speed measured in meters per second (returned if available)
timestamp The response date/time (returned if available)

Geolocation object - Other interesting Methods


 

The Geolocation object is also known to have other interesting methods:

The example given below will show the watchPosition() method. You must be having an accurate GPS device for testing this (like iPhone):

Example

<script>
var 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>
Try it Yourself