Navigator geolocation Property - Javascript Browser Object Model

Javascript examples for Browser Object Model:Navigator

Description

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>
<html>
<body>

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

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

<script>
var x = document.getElementById("demo");

function getLocation() {/*www  . j ava  2  s  . c  o m*/
    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>

</body>
</html>

Related Tutorials