HTML5 have cool feature that is provide gelocation of the user on the fly without using any extra services that we do right now. This feature is supported by Gelocation API which is part of HTML5.
Following is sample Code in JavaScript how it works.
Below is handleError function that used to handle any error which might occur while accessing GeoLocation of the user.
Update GeoLocation as you Move with watchPosition
There is one of function which is part of Gelocation API that is very useful to get continues update of location as user moves from one place to another palce.
watchPosition - function that is used to achieve the above function.
To see how this works actually just replace getCurrentPosition function with watchPosition.
To stop updates from watchPosition you can use clearWatch() of API.
Position Object
Both method returns Postion object as output. Following is list of property that is part of Position Object.
Leave comment if you have any query or if you like it.
Following is sample Code in JavaScript how it works.
<button onclick="getGeoLocation()">Get GeoLocation</button> <script> function getGeoLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition,HandleError); } else { alert("Geolocation is not supported by this browser."); } } function showPosition(position) { alert("Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude); } </script>Code is work in this pattern.
- First when user click on button it calls getGeoLocation
- In that function its first check navigator.geolocation supported or not.
- It calls Inbuilt JavaScript method getCurrentPosition which returns Postion object
- This Position object used by showPostion to display Latitude and Logitude
Below is handleError function that used to handle any error which might occur while accessing GeoLocation of the user.
function HandleError(error) { switch(error.code) { case error.PERMISSION_DENIED: alert("User denied the request for Geolocation."); break; case error.POSITION_UNAVAILABLE: alert("Location information is unavailable."); break; case error.TIMEOUT: alert("The request to get user location timed out."); break; case error.UNKNOWN_ERROR: alert("An unknown error occurred."); break; } }Code of the function is self-explained by the values given.
Update GeoLocation as you Move with watchPosition
There is one of function which is part of Gelocation API that is very useful to get continues update of location as user moves from one place to another palce.
watchPosition - function that is used to achieve the above function.
To see how this works actually just replace getCurrentPosition function with watchPosition.
To stop updates from watchPosition you can use clearWatch() of API.
Position Object
Both method returns Postion object as output. Following is list of property that is part of Position Object.
coords.latitude | The latitude as a decimal number |
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.altitudeAccuracy | The altitude accuracy of position |
coords.heading | The heading as degrees clockwise from North |
coords.speed | The speed in meters per second |
timestamp | The date/time of the response |
the function handleError should be HandleError.
ReplyDeletefunction HandleError(){ ... }
Thanks for that its updated now...
DeleteWow, this has become so easy.
ReplyDeleteNice post!