Geolocation position attribute

Definition and Usage

The position attribute returns the location and altitude of the device on Earth.

Example

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

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

Try it yourself

position attribute

egenskaper Description
position.coords Return the Coordinates object defining the current position.
position.timestamp Return the DOMTimeStamp object representing the time the position was retrieved.

Webbläsarstöd

egenskaper Chrome IE Firefox Safari Opera
position 5.0 9.0 3.5 5.0 16.0