Javascript Browser Navigator geolocation Property

Introduction

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

Click the button to get your coordinates.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="getLocation()">Test</button>
<p id="demo"></p>

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

function getLocation() {/*from w  w w.  ja va 2  s .  co 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>

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

This property is read-only.




PreviousNext

Related