The Geolocation API provides a method for determining the exact (or not very accurate) location of a user.
This can be useful in many cases – for example, to provide the user with geo-dependent information or to create routes.
Although this API is not part of HTML5 and was developed by the W3C and not the WHATWG – a description of this API was added to the HTML5 book by Remy Sharp and Bruce Lawson. Therefore, we also decided to talk about it.
This API is very easy to use, so the purpose of this article is to show the reader how simple it is.
browser compatibility
The W3C Geolocation API is currently supported by the following browsers:
Firefox 3.5+
Chrome 5.0+
Safari 5.0+
Opera 10.60+
Internet Explorer 9.0+
The Geolocation API is also supported by the following mobile browsers:
Android 2.0+
iPhone 3.0+
Opera Mobile 10.1+
Symbian (S60 3rd & 5th generation)
Blackberry OS 6
Maemo
data protection
The specification clearly indicates that the nature of the API exposes the user’s location, which could put the user at risk of privacy. Therefore, before determining the location, the user must consent to receive data about his location. Browsers take care of this through pop-ups.
location data sources
Various technologies can be used to determine the user’s location. Naturally, different technologies allow different degrees of accuracy to be achieved. Desktop browsers can use WiFi (accuracy up to 20 meters) or IP Geolocation, which can only name the user’s city and sometimes does not work as it should. Mobile devices can use trigonometric techniques such as GPS (accuracy up to 10 meters), WiFi or GSM / CDMA identifiers (accuracy up to 1000 meters).
using api
Before you start using the Geolocation API, you first need to determine if the browser supports this feature. The API defines a function to define support:
if (navigator.geolocation) {
// do fancy stuff
}
If the function returns false, then you should inform the user that he has a defective browser and laugh in his face (just kidding).
As for the API itself, there are two functions to get the user’s location:
getcurrentposition and watchposition
Both of these methods execute immediately and wait asynchronously to get the current location. They take the same number of arguments, 2 of which are optional:
successCallback is a function called if the method is successfully executed.
[errorCallback] – a function called if the method was called with an error.
[options] – a set of options:
enableHighAccuracy is a flag indicating that the best accuracy is required. Specifying this attribute can cause a long delay, as well as more power consumption on mobile devices, since the GPS module may be involved. The variable type is Boolean. The default is false.
timeout – indicates the maximum time to wait for a response. The default is 0 – infinite.
maximumAge – Specifies the maximum time to keep location data in the cache. Specified with milliseconds, default 0, i.e. each time the function is called, the location will be redefined.
Before comparing these two methods, there is one more method that needs to be explained.
clearwatch
This method takes one argument, watchID, which defines the location process (this ID is returned by the watchPosition function).
Now let’s look at the main differences between getCurrentPosition and watchPosition. The difference is that watchPosition will inform the code every time the user’s position changes. This is very useful if users are moving and you need to track the trajectory of their movement. getCurrentPosition specifies the location once. This method also returns watchID.
When the user’s location is determined, the Position object becomes available to you, which contains several properties:
Property
Description
coords.latitude
Latitude degrees
coords.longitude
Longitude degrees
coords.altitude
Height in meters above the reference ellipsoid.
coords.accuracy
The accuracy of the result in meters. The value of this property informs the application how accurate the resulting latitude and longitude coordinates are. Using this property, you can determine whether the accuracy of the received data is suitable, for example, to determine the user’s street, or whether only the city can be determined.
coords.altitudeAccuracy
Accuracy of height above the reference ellipsoid.
coords.heading
The direction of movement of the device is measured clockwise from north.
coords.speed
The current speed of movement of the device in meters per second.
timestamp
The time the location was received.
Not all of these properties are required. Only coords.latitude, coords.longitude and coords.accuracy will always be specified, the rest can be null. The first 2 properties can be used, for example, to indicate a location on Google Maps.