Window navigator.geolocation
Example
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 »
Description
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.
Note
Geolocation is much more accurate for devices with GPS, like smartphones.
See Also:
Syntax
navigator.geolocation
Return Value
Type | Description |
An object | A reference to a Geolocation object. |
Browser Support
navigator.geolocation
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |