HTML5 Geolocation

What is Geolocation?

  • The Geolocation API lets you share your location with the websites.
  • JavaScript will capture the latitude and longitude and send it to the backend web server.
  • Today most of the browsers and mobile devices support the Geolocation API.
They work with a new property of the global navigator object i.e. Geolocation object which is as follows:

var geolocation = navigator.geolocation;

Where,
     geolocation is the service object that allows the widgets to retreive information about the geographic location of the device.

Geolocation Methods

Following are the methods provided by the geolocation object:

1. getCurrentPosition() - It is used to retrieve the current geographic location of the user.
2. watchPosition() - It receives the periodic update about the current geographic location of the device.
3. clearWatch() - It is a method that cancels an ongoing watchPosition call.

Location Properties

  • The methods getCurrentPosition() and getPositionUsingMethodName() are used for specifying the callback method which will retrieve the information of the location.
  • Both the methods are called asynchronously by using an object Position that stores the complete information about the locations.
  • The current geographic location of the device is specified by the Position object.
The properties of the Position object are as follows:
If any value is not provided by the system then the value will be set to null for all the optional properties.

PropertyTypeDescription
coordsobjectsThe geographic location of the device is specified. A set of geographic coordinates together with the information about the heading and the speed is expressed as the location.
coords.latitudeNumberIt specifies the latitude which is estimated in decimal degrees. The range of the values are [-90.00, +90.00].
coords.longitudeNumberIt specifies the longitude which is estimated in decimal degrees. The range of the values are [-180.00, +180.00].
coords.altitudeNumberIt is an optional property which specifies the altitude estimate in meters above the WGS 84 ellipsoid.
coords.accuracyNumberIt is an optional property which specifies the accuracy of the latitude and longitude which is estimated in meters.
coords.altitudeAccuracyNumberIt is an optional property which specifies the accuracy of the altitude estimated in meters.
coords.headingNumberIt is an optional property which specifies the current direction of movement of the device in degrees counting clockwise relative to the true north.
coords.speedNumberIt is an optional property which specifies the current ground speed of the device in meters per second.
timestampdateThe time is specified when the location of the information was retrieved and the position object is created.

Error Handling

  • Geolocation is one of the most complicated things. The errors should be caught and handled carefully.
  • The methods getCurrentPosition() and watchPosition() make use of the PositionError object.
This object has two properties:

a) code – It is a number type. It contains a numeric code for the error.
b) message – It is a type of string.  It contains a human-readable description of the error.

The error codes returned in the PositionError object are as follows:

Code NumberConstantDescription
0UNKNOWN_ERRORThis method is used when the retrieval of location of the device fails due to an unknown error.
1PERMISSION_DENIEDThis method is used for retrieving the failed location of the device as the application does not have any permission to use the Location Service.
2POSITION_UNAVAILABLEThis method tells us that the location of the device could not be determined.
3TIMEOUTIn this method it is difficult to retrieve the information about the location within the specified maximum timeout interval.

Position Options

Syntax for getCurrentPosition() method:
getCurrentPosition(callback, ErrorCallback, options)

Where,
     The third argument “options” is the “PositionOptions” object which helps in specifying the set of options used for retrieving the geographic location of the device.

Options that can be specified as a third party argument are as follows:

PropertyTypeDescription
enableHighAccuracyBooleanThis property specifies if the widget wants to receive the accurate location. The default value is false.
timeoutNumberThis property is the number of milliseconds that the web application is willing to wait for a certain position.
maximumAgeNumberThis property specifies the expiry time in milliseconds for the cached location information.