Javascript Browser Geolocation position Property

Introduction

The position property returns the position and altitude of the device on Earth.

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 ww  .  ja va2  s .  c om*/
  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>

Coordinates Properties

Property Description
position.coordsa Coordinates object that defines the current location
position.timestamp a DOMTimeStamp representing the time when the location was retrieved



PreviousNext

Related