What is Geolocation?
They work with a new property of the global navigator object i.e. Geolocation object which is as follows:
var geolocation = navigator.geolocation;
- The Geolocation API lets you share your location with the websites.
- Today most of the browsers and mobile devices support the Geolocation API.
geolocation is the service object that allows the widgets to retreive information about the geographic location of the device.
Geolocation MethodsFollowing 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.
The properties of the Position object are as follows:
- 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.
If any value is not provided by the system then the value will be set to null for all the optional properties.
|coords||objects||The 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.latitude||Number||It specifies the latitude which is estimated in decimal degrees. The range of the values are [-90.00, +90.00].|
|coords.longitude||Number||It specifies the longitude which is estimated in decimal degrees. The range of the values are [-180.00, +180.00].|
|coords.altitude||Number||It is an optional property which specifies the altitude estimate in meters above the WGS 84 ellipsoid.|
|coords.accuracy||Number||It is an optional property which specifies the accuracy of the latitude and longitude which is estimated in meters.|
|coords.altitudeAccuracy||Number||It is an optional property which specifies the accuracy of the altitude estimated in meters.|
|coords.heading||Number||It is an optional property which specifies the current direction of movement of the device in degrees counting clockwise relative to the true north.|
|coords.speed||Number||It is an optional property which specifies the current ground speed of the device in meters per second.|
|timestamp||date||The time is specified when the location of the information was retrieved and the position object is created.|
This object has two properties:
a) code –
- 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.
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:
|0||UNKNOWN_ERROR||This method is used when the retrieval of location of the device fails due to an unknown error.|
|1||PERMISSION_DENIED||This method is used for retrieving the failed location of the device as the application does not have any permission to use the Location Service.|
|2||POSITION_UNAVAILABLE||This method tells us that the location of the device could not be determined.|
|3||TIMEOUT||In this method it is difficult to retrieve the information about the location within the specified maximum timeout interval.|
Position OptionsSyntax for getCurrentPosition() method:
getCurrentPosition(callback, ErrorCallback, options)
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:
|enableHighAccuracy||Boolean||This property specifies if the widget wants to receive the accurate location. The default value is false.|
|timeout||Number||This property is the number of milliseconds that the web application is willing to wait for a certain position.|
|maximumAge||Number||This property specifies the expiry time in milliseconds for the cached location information.|