What is it?
Websites that use location aware browsing will ask you if you want to share your current location as a context to bring you more relevant information. If you consent, it sends a request to a geolocation service provider to get an estimate of your location (latitude and longitude). If you do not consent, it will not do anything. Location aware browsing should always be opt-in and no location information should ever be sent without your permission.
How does it work?
A geolocation service provider will try to collect information using IP geolocation, cell tower triangulation, nearby wireless access points and/or GPS. The exact methods will depend on the service provider used, your hardware and your current location itself. For example IP Geolocation has a city-to-country accuracy and is widely available, while GPS is extremely accurate however is less common. As a result the accuracy of the returned location can vary greatly.
What does the API look like?
The W3C Geolocation API Specification (currently a W3C editor's draft) defines a simple API that provides scripted access to geographical location information associated with the hosting device. The specification is scheduled to become a recommendation in Q4 2009.
The geolocation API is published through a geolocation child object within the navigator object:
if (typeof navigator.geolocation != "undefined") {
// geolocation is available, show contextual options
}
else {
// no geolocation available, show default options
}
To get the current location and use it in a custom callback function:
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
function successCallback(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
}
function errorCallback(error) {
var code = error.code;
var message = error.message;
}
If you want to keep track of changes in the user's location:
var watchId = navigator.geolocation.watchPosition(successCallback, errorCallback);
function successCallback(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
}
function errorCallback(error) {
var code = error.code;
var message = error.message;
}
Which can be cleared with:
navigator.geolocation.clearWatch(watchID);
Which browsers support it?
The following browsers support the W3C Geolocation API:
Google offers two additional ways to location-enable your web apps:
- Gears Geolocation API via the Google Gears plug-in or Google Chrome
- Google AJAX API property
While the Geolocation API is based on the W3C Geolocation API, the AJAX API property has it's own syntax. There are already various wrapper libraries available to get the most out of location aware browsing today [1][2].
Conclusion
Location aware browsing is an innovative new feature that you can already start using today, while in the coming years devices, browsers and services will only improve. A user's current location can be very interesting to be used as a context for higher relevancy or for navigational purposes. Interested? Go check it out today!