HTML5 - Geolocation

HTML5 Geolocation API lets you share your location with your favorite web sites. A JavaScript can capture your latitude and longitude and can be sent to backend web server and do fancy location-aware things like finding local businesses or showing your location on a map.
Today most of the browsers and mobile devices support Geolocation API. The geolocation APIs work with a new property of the global navigator object ie. Geolocation object which can be created as follows −
var geolocation = navigator.geolocation;

The geolocation object is a service object that allows widgets to retrieve information about the geographic location of the device.
The geolocation object provides the following methods −
This method retrieves the current geographic location of the user.
This method retrieves periodic updates about the current geographic location of the device.
This method cancels an ongoing watchPosition call.
Following is a sample code to use any of the above method −
function getLocation() {
   var geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler);
}
Here showLocation and errorHandler are callback methods which would be used to get actual position as explained in next section and to handle errors if there is any.
Geolocation methods getCurrentPosition() and getPositionUsingMethodName() specify the callback method that retrieves the location information. These methods are called asynchronously with an object Position which stores the complete location information.
The Position object specifies the current geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed.
The following table describes the properties of the Position object. For the optional properties if the system cannot provide a value, the value of the property is set to null.
Following is a sample code which makes use of Position object. Here showLocation method is a callback method −
function showLocation( position ) {
   var latitude = position.coords.latitude;
   var longitude = position.coords.longitude;
   ...
}
Geolocation is complicated, and it is very much required to catch any error and handle it gracefully.
The geolocations methods getCurrentPosition() and watchPosition() make use of an error handler callback method which gives PositionError object. This object has following two properties −
The following table describes the possible error codes returned in the PositionError object.
Following is a sample code which makes use of PositionError object. Here errorHandler method is a callback method −
function errorHandler( err ) {
   
   if (err.code == 1) {
      
      // access is denied
   }
   ...
}
Following is the actual syntax of getCurrentPosition() method −
getCurrentPosition(callback, ErrorCallback, options)

Here third argument is the PositionOptions object which specifies a set of options for retrieving the geographic location of the device.
Following are the options which can be specified as third argument −
Following is a sample code which shows how to use above mentioned methods −
function getLocation() {
   var geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler, {maximumAge: 75000});