Get the latitude and longitude of the user's position:
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 »
The geolocation
property returns a Geolocation object that can be used to locate the user's position.
The geolocation
property is read-only.
The geolocation
property is only available in secure contexts (HTTPS).
The geolocation
property is only available if the user approves it.
Geolocation is much more accurate for devices with GPS, like smartphones.
navigator.geolocation
Type | Description |
An object | A reference to a Geolocation object. |
navigator.geolocation
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!