Blog: News, views and some opinionated commentary from the team at Mecca Medialight


Lost on ice

Geolocation: Where's my head at?

So you are building a site or app, and you want to offer content or services that can be tailored to users according to their location (eg 'find the nearest cinema'). Geolocation lets users share their location with trusted web sites. The Geolocation API defines a "high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude". The API itself is "agnostic of the underlying location information sources". It will use information that can come from a range of sources, including Global Positioning System (GPS) and location inferred IP , RFID, WiFi and Bluetooth MAC addresses, as well as user input. Generally, mobile devices with GPS will provide more accurate location details - but you may be surprised at what your desktop knows about you.

Obviously, sharing your location with the world raises some privacy concerns. The Geolocation API Specification states that browsers "must not send location information to Web sites without the express permission of the user" - so your browser should request your permission before making location information available to websites.

You can try it out by clicking the "Locate Me" button below (...and if you don't see the button, then your browser does not support location services).

Whilst there are some inconsistencies with older phone and Internet Explorer (before v.9) there is an open source Mobile and Desktop Javascript Library for Geolocation Fallback that makes implementing geolocation a breeze on a site designed for viewing on a range of devices. Here is the basic code required

if (geoPosition.init()) {
    // location services available!
    var options = {enableHighAccuracy: true};
    geoPosition.getCurrentPosition(success_callback, error_callback, options);
} else {
    // location services not available
}

function success_callback(p) {
    var latitude = parseFloat(p.coords.latitude).toFixed(2);
    var longitude = parseFloat(p.coords.longitude).toFixed(2);
    alert("Your location: lat " + latitude + " long " + longitude);
}

function error_callback(p) {
    alert("Unable to determine your location: " + p.message);	
}

Firstly, initialise the location services using geoPosition.init. If this returns false, then location services are not available. If the location services are available, you can then call getCurrentPosition, passing a success and error callback. The successful callback will return an object with two properties: coords and timestamp. The coords object has properties

  • coords.latitude
  • coords.longitude
  • coords.accuracy

The latitude and longitude attributes are geographic coordinates specified in decimal degrees. The accuracy attribute denotes the accuracy level of the latitude and longitude coordinates and is specified in meters.

Depending on your device and how gets its location information, additional properties might be provided. These can include

  • coords.altitude
  • coords.accuracy
  • coords.altitudeAccuracy
  • coords.heading
  • coords.speed

Alternatives

Rather than asking the user for their location (which is essentially how location services works) - you can make assumptions about a user based on their host IP address. Typically, this is used to

  • Tailor language and content
  • Route traffic
  • Customise advertising
  • Enforce digital rights
  • Traffic analysis

There are commercial and free databases that map IP addresses to country, state and even city. The accuracy of these databases varies. For example, one database suggests I am currently in Perth, while another (probably created by a magpie fan) placed me in nearby Collingwood. Generally, mapping the IP will get the country right - although one client in NSW told me they show up as being in New Zealand (where they have an office and presumably where their proxy server is located). Some geo-IP vendors include

For example, here's an assessment of your location (using the free database from MaxMind)

Loading...

This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com.