HTML DOM Navigator geolocation Property
Last Updated :
14 Jun, 2023
The Navigator geolocation property is used to return a geolocation object by the browser which can be used to locate a user’s position.
It is a read-only property and is only available on the approval of the user since it can compromise the user’s privacy.
Syntax:
navigator.geolocation
Below program illustrates the Navigator geolocation Property :
Checking the user’s geolocation.
HTML
<!DOCTYPE html>
< html >
< head >
< title >Navigator geolocation Property in HTML</ title >
< style >
h1 {
color: green;
}
h2 {
font-family: Impact;
}
body {
text-align: center;
}
</ style >
</ head >
< body >
< h1 >GeeksforGeeks</ h1 >
< h2 >Navigator geolocation Property</ h2 >
< p >For checking your coordinates,
double click the "Get Coordinates" button : </ p >
< button ondblclick = "getCoordinates()" >Get Coordinates</ button >
< p id = "loc" ></ p >
< script >
var x = document.getElementById("loc");
function getCoordinates() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showcoordinates);
} else {
x.innerHTML = "The browser doesn't support Geolocation.";
}
}
function showcoordinates(myposition) {
x.innerHTML = "Your Latitude is: " + myposition.coords.latitude +
"< br >Your Longitude is: " + myposition.coords.longitude;
}
</ script >
</ body >
</ html >
|
Output:
Before clicking the button:
After clicking the button:
Supported Browsers: The browser supported by Navigator geolocation are listed below:
- Google Chrome 5
- Edge 12
- Internet Explorer 9
- Firefox 3.5
- Opera 10.6
- Safari 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...