Navigator geolocation Property - Javascript Browser Object Model

Javascript examples for Browser Object Model:Navigator


The geolocation property returns a Geolocation object to locate the user's position.

The position is not available unless the user approves it.

This property is read-only.

Return Value

A reference to the Geolocation object

The following code shows how to get the latitude and longitude of the user's position:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<button onclick="getLocation()">Test</button>

<p id="demo"></p>

var x = document.getElementById("demo");

function getLocation() {/*www  . j ava  2  s  . c  o m*/
    if (navigator.geolocation) {
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;


Related Tutorials