Browser Compatibility
- Firefox 3.5+
- Chrome 5.0+
- Safari 5.0+
- Opera 10.60+
- Internet Explorer 9.0+
- Android 2.0+
- iPhone 3.0+
- Opera Mobile 10.1+
- Symbian (S60 3rd & 5th generation)
- Blackberry OS 6
- Maemo
Data Protection
Geolocation sources
Using the API
if (navigator.geolocation) {// do fancy stuff}
getCurrentPosition and watchPosition
successCallback – called if the method returns successfully
[errorCallback] – called if the method returns with an error
- [options] – a number of options are available:
enableHighAccuracy– provides a hint that the application would like the best possible results. This may cause a slower response time and in the case of a mobile device, greater power consumption as it may use GPS. Boolean with a default setting of false.timeout– indicates the maximum length of time to wait for a response. In milliseconds with a default of 0 – infinite.maximumAge– denotes the maximum age of a cached position that the application will be willing to accept. In milliseconds, with a default value of 0, which means that an attempt must be made to obtain a new position object immediately.
clearWatch
watchID of the watch process to clear (which is returned by watchPosition)getCurrentPosition andwatchPosition is that watchPosition keeps informing your code should the position change, so basically it keeps updating the user’s position. This is very useful if they’re on the move and you want to keep track of their position, whereas getCurrentPosition is a once off. This method also returns a watchID which is required when you want to stop the position constantly being updated by callingclearWatch method is called.Positionobject which contains a number of properties:| Property | Details |
|---|---|
| coords.latitude | Decimal degrees of the latitude |
| coords.longitude | Decimal degress of the longitude |
| coords.altitude | Height in metres of the position above thereference ellipsoid |
| coords.accuracy | The accuracy in metres of the returned result. The value of this setting informs the application how useful the returned latitude/longitude value actually is. This can help in determining if the returned result is accurate enough for the purpose it is intended for, e.g. values for streetview locations will need to be more accurate than those for a country based location |
| coords.altitudeAccuracy | The accuracy in metres of the returned altitude |
| coords.heading | Direction of travel of the hosting device, clockwise from true north |
| coords.speed | The current ground speed of the hosting device in metres per second |
| timestamp | Timestamp of when the position was acquired |
coords.latitude, coords.longitudeand coords.accuracy are guaranteed to be returned (all others may be null), and the first two are by and large the most relevant, as it is from these that a position can be plotted on a Google Map, for example.Position object and the coordinates interface on the W3 specification itself.Putting it all together
displayPosition on success which simply pops up an alert box with the captured latitude and longitude:if (navigator.geolocation) {var timeoutVal = 10 * 1000 * 1000;navigator.geolocation.getCurrentPosition(displayPosition,displayError,{ enableHighAccuracy: true, timeout: timeoutVal,maximumAge: 0 });}else {alert("Geolocation is not supported by this browser");}function displayPosition(position) {alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);}
displayError method when attempt to fetch the user’s location data. This function simple converts the returned error code to an appropriate message:function displayError(error) {var errors = {1: 'Permission denied',2: 'Position unavailable',3: 'Request timeout'};alert("Error: " + errors[error.code]);}
A final word
- Finding and plotting points of interest in the user’s area
- Annotating content with location informationShowing a user’s position on a map (helping with directions of course!)
- Turn-by-turn route navigation – using
watchPosition - Up-to-date local information – updates as you move
Examples
- Plot a location on a Google Map using
getPosition - Plot and update a location on a Google Map using
watchPosition– this is best viewed on a moving device so you can see the position updating
