Latest web development tutorials
 

Navigator geolocation Property

< Navigator Object

Example

Get the latitude and longitude of the user's position:

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;
}

The result of x could be:

Latitude: 40.7242737
Longitude: -73.9802387
Try it Yourself »

Definition and Usage

The geolocation property returns a Geolocation object that can be used to locate the user's position.

Since this can compromise user privacy, the position is not available unless the user approves it.

Note: This property is read-only.

For more information about Gelocation, visit our HTML5 Gelocation Tutorial.


Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
geolocation 5.0 9.0 3.5 5.0 16.0

Note: Geolocation is much more accurate for devices with GPS, like a smartphone.


Syntax

navigator.geolocation

Technical Details

Return Value: A reference to the Geolocation object

< Navigator Object