How to use geolocation in html5









There is given belwo syntax for location calculating in html5

Use the getCurrentPosition() method to get the user’s position.

var x = document.getElementById(“demo”);
function getLocation() {
if (navigator.geolocation) {
} else {
x.innerHTML = “Geolocation is not supported by this browser.”;
function showPosition(position) {
x.innerHTML = “Latitude: ” + position.coords.latitude +
“<br>Longitude: ” + position.coords.longitude;

First it would check the support for geolocation
If it is supported then run the getCurrentPosition() method.
If not, display a message to the user
If the getCurrentPosition() method is processed Then it returns a coordinates object
to the function specified in the parameter ( showPosition )
showPosition() function gets the displays the Latitude and Longitude

The getCurrentPosition() Method – Return Data

The getCurrentPosition() method returns an object if it is successful. The latitude, longitude and accuracy properties are always returned. The other properties below are returned if available.
Property Description
coords.speed The speed in meters per second
timestamp The date/time of the response
coords.longitude The longitude as a decimal number
coords.accuracy The accuracy of position
coords.altitude The altitude in meters above the mean sea level
coords.latitude The latitude as a decimal number
coords.altitudeAccuracy The altitude accuracy of position
coords.heading The heading as degrees clockwise from North