HTML5 Geolocation

HTML5 Tutorial

HTML5 Geolocation

Completed

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

Locate the User's Position

Completed

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.

Browser Support

Completed

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

Using HTML Geolocation

Completed

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:

  • First you need to check whether Geolocation is supported
  • If it is supported, you need to run the getCurrentPosition() method. If not, displaying a message to the user is necessary
  • If the getCurrentPosition() method is working, it will return a coordinates object to the function mentioned in the parameter (showPosition)
  • The showPosition() function will give the outputs of Latitude and Longitude

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

Handling Errors and Rejections

Completed

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

Completed

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:

  • get Up-to-date information about a local point
  • To find out Points-of-interest near the user
  • Turn-by-turn navigation (GPS)

The getCurrentPosition() Method - Return Data

Completed

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

Completed

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

  • watchPosition() - It will return the current position of the user and will continue to return even the updated position as the user moves (similar to the GPS in a car).
  • clearWatch() - It will stop the watchPosition() method.

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