Navigator geolocationProperty

Navigator 对象参考手册Navigator Object

Definition and Usage

The Navigator geolocation property returns a Geolocation object, through which the device's location information can be accessed, allowing websites or applications to provide personalized results based on the user's location.

The geolocation property can only be used under HTTPS.

The geolocation property can only be used after the user grants permission.

Navigator geolocation is a read-only property.

For more information, please refer toHTML5 Geolocation。

Syntax

navigator.geolocation


Browser Support

All major browsers support the geolocation property.

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

Example

Example

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition);
} else {
  document.getElementById("demo").innerHTML =
  "Geolocation is not supported by this browser.";
}

function showPosition(position) {
  document.getElementById("demo").innerHTML =
  "Latitude: " + position.coords.latitude +
  "Longitude: " + position.coords.longitude;
}

Try it yourself »
Navigator 对象参考手册Navigator Object Other Extensions