HTML5 Geolocation
HTML5 Geolocation
Locate the User's Position
The HTML GeolocationHTML5 Geolocation
Locate the User's Position
The HTML Geolocation API is used to get the geographical position of a user.
Since this can compromise privacy, the position is not available unless the user approves it.
APIGeolocation5.0 - 49.0 (http)
50.0 (https)9.03.55.016.0
Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS. If your site is hosted on an non-secure origin (such as HTTP) the requests to get the users location will no longer function.
Using HTML Geolocation
The getCurrentPosition() method is used to return the user's position.
The example below returns the latitude and longitude of the user's position:
Example
<script>
var x = document.getElementById("demo");
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
functionshowPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
Example explained:
Check if Geolocation is supportedIf supported, run the getCurrentPosition() method. If not, display a message to the userIf the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition)The showPosition() function outputs the Latitude and Longitude
The example above is a very basic Geolocation script, with no error handling.
Handling Errors and Rejections
The second parameter of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user's location:
API is used to get the geographical position of a user.
Since this can compromise privacy, the position is not available unless the user approves it.
APIGeolocation5.0 - 49.0 (http)
50.0 (https)9.03.55.016.0
Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS. If your site is hosted on an non-secure origin (such as HTTP) the requests to get the users location will no longer function.
Using HTML Geolocation
The getCurrentPosition() method is used to return the user's position.
The example below returns the latitude and longitude of the user's position:
Example
<script>
var x = document.getElementById("demo");
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
functionshowPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
Example explained:
Check if Geolocation is supportedIf supported, run the getCurrentPosition() method. If not, display a message to the userIf the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition)The showPosition() function outputs the Latitude and Longitude
The example above is a very basic Geolocation script, with no error handling.
Handling Errors and Rejections
The second parameter of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user's location:
This post has received a 1.56 % upvote from @drotto thanks to: @rabiul21.