Geolocation on the web

Did you know W3C has a draft specification for Geolocation API in browsers? In practice, that means a JavaScript call could discover your location. Does that really work? And if, how?

There’s this site called http://isgeolocationpartofhtml5.com/, which plots your position on a Google Map. It also correctly answers the question of whether Geolocation API belongs to HTML5; it does not, but is rather a separate specification.

image

If you did try the site, you may have noticed one of a few things:

  1. If you’re running IE, you will get plotted into a random location. IE does not support the API.
  2. If you’re running Firefox 3.5 or newer, you will get a prompt asking if you want to share your location. If you answer no, you’ll get a random location; if you answer “yes”, you may get a correct location.
  3. Other browsers provide varying results. Most builds don’t support it.

How do they do it?

The Geolocation API discusses the ways to call this. In terms of JavaScript code, it’s fairly simple:

if ( navigator.geolocation ) {
    navigator.geolocation.getCurrentPosition( successPosition, errorPosition );
} else {
    errorPosition();
}

Not very hard – just passing a couple of . The interesting part is the success path, i.e. how does the browser know where I am?

The underpinnings of geolocation

image Superficially speaking, geolocation is a reasonably simple problem – it’s just a couple of coordinates. The key problem is that the location data isn’t exactly trivial to come by. Typically, devices do not know where they are. The notable exceptions are mobile devices with built in GPS, but even that is not a perfect solution: GPS works poorly indoors, cannot be kept always on due to battery consumption and takes quite a while to boot up after having been off.

Instead, there are a few other strategies:

  • Locating by IP address. This is by far the most typical, but can also be very inaccurate at times. Its greatest benefit is that it can be done without the client. The browser needs no geolocation support, as the IP address is always known to the server.
  • Using known Wi-Fi hotspots. If the computer has its WLAN capabilities on, it’ll notice nearby wireless networks. If you happen to know the locations of these stations, you instantly gain an idea on where you are. Not surprisingly, Google Street View photography vehicles also tracked the wireless networks and stored their locations.
  • Using cell tower information. If you’re connected to the cellular network – typical with phones, less common with computers – you can use the cell tower ID to identify the tower and thus its position – if you happen to possess a mapping of cell tower IDs to locations. Not surprisingly, e.g. Google does.
  • Using GPS, perhaps A-GPS (Assisted GPS). In A-GPS, other information sources are used to provide a rough location, greatly reducing the GPS startup time.

Combinations are, of course, possible. For example, Firefox sends your IP and nearby Wi-Fi data to Google, which Firefox uses as its data provider for geolocation. That’s a decent combination and has worked excellently a few times for me; other times, the combination positions me in New York while I’m really across the Atlantic in Finland.

The Windows 7 Geolocation Support in Context

One of the key improvements for Windows 7 was that there is now a new Windows Sensor and Location Platform. What it means is that there is an OS-level library for providing geolocation data to the applications.

Well, how does Windows know where you are? The truth is, it doesn’t. The WSLP provides a standard way to plug sensors into the OS, enabling access from all applications without the applications knowing the actual source of the information – quite a bit like the W3C Geolocation specification.

imageIn reality, rather few implementations for this sensor API exist yet. Perhaps the most noteworthy is Geosense for Windows, a free software-based location driver for Windows 7. At this time, its positioning functionality is the same as it is with Firefox: Google’s services are used.

Ideally,  Firefox would request the location data from the Windows 7 SLP (as represented in the diagram) instead of directly accessing Google services. This way, Firefox would directly benefit from possible better implementations – such as the ones based on GPS hardware. Not surprisingly, such a plan is in the works.

Of course, since the vast majority of Windows 7 installations don’t provide any data through the SLP right now, the benefit wouldn’t be great – and for OSes other than Windows 7, you’d still need the handwritten implementation. Of course, the benefits of SLP are on the rise as hardware manufacturers start writing drivers to power the SLP.

May 28, 2010 · Jouni Heikniemi · No Comments
Tags: , , ,  · Posted in: Web

Leave a Reply