Using the GeoLocation API

Warning This article was written over six months ago, and may contain outdated information.

With the rapid growth of the mobile web, location-aware services are very much in-demand; the GeoLocation API was proposed to cater to this need.

Implementation is spotty at the moment; Firefox 3.5 supports it, as does Safari for iPhone (although not on the desktop, AFAICS). But it’s so simple to use, I’ve no doubt it will be adopted rapidly.

I’ve put together a small demo of the GeoLocation API in action; it uses data from the Arts Council England website, so visitors from outside of England won’t get any meaningful results from this, I’m afraid. Visitors from England will probably get best results using a mobile device (such as an iPhone).

First, the JavaScript function tests that the browser supports the API:

var hasGeoLocation = navigator.geolocation ? true : false;
if (hasGeoLocation === true) { ... }

If so, it requests the current position from the browser using the getCurrentPosition method (the user will have to give permission for this) and passes the result into a function:

navigator.geolocation.getCurrentPosition(function(position) {

This result is used in the latitude and longitude attributes of the coords object, which I then use for the rest of my function (using jQuery’s getJSON function to mine microformatted data from the map page of the Arts Council England website, transforming it using Optimus, then parsing it back into HTML for my demo).

There’s a lot more to this, of course; the API is quite extensive, covering altitude and heading as well as lat/long, and there’s a massive amount of geotagged data available (Flickr photos, for example). Hopefully my demo serves as an example of how easy it is to make the move into the mobile space.

1 comment on
“Using the GeoLocation API”

  1. Thanks! There is an article highlights the capabilities and constraints of this new JavaScript API which is based on wireless geolocation and compare it with the server-side IP geolocation at .