
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:
Leave HTML5 Geolocation to:
Read more #html posts
Best Posts From Md Robiul Islam
We have not curated any of rabiul21's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Md Robiul Islam
- ABOUT BANGLADESH
- Why Study JavaScript?
- Nice Doll
- Photography
- HTML5 Geolocation
- HTML Plug-ins
- HTML5 Audio
- HTML Multimedia
- HTML Multimedia
- HTML5 Canvas
- HTML5 Semantic Elements
- HTML5 New Elemen New
- HTML5 Browser Support
- HTML5 Introduction
- HTML Input Attributes
- HTML Form Elements
- HTML Forms
- HTML Symbol Entities
- HTML Entities
- HTML Computer Code Elements