TheDotProduct

Web-tech nerd stuff

How to get an accurate Geo location from Apple IPhone using navigator.geolocation.watchPosition

I’ve been messing around for a few weeks now with some emerging standards such as HTML5 and the new navigator.geolocation functionality.

Anyone who’s tried using navigator.geolocation on a fixed-line (i.e. not mobile) device will know that it’s not too useful, usually at home my location is given as New York, USA when in fact I am in Oxfordshire in the UK! So, I turned to my IPhone and had a bit of a hard time at first trying to get an accurate position reading from navigator.geolocation.getCurrentPosition(). My findings here refer to using JavaScript in Safari on Apple IPhone (IPhone 3G with OS 3.1.3 on O2′s GSM network in the UK).

I did a little reading of the navigator.geolocation standard and found another available method: navigator.geolocation.watchPosition() which after a few readings were taken, gave a much more accurate reading of my Geo location.

navigator.geolocation.watchPosition() differs from navigator.geolocation.getCurrentPosition() in that it executes a function of your choosing each time the Geo location of the device changes instead of only executing your function only once. It turns out that for the IPhone at least, this is a very important distinction.

The reason that navigator.geolocation.watchPosition() is able to give a much more accurate reading than navigator.geolocation.getCurrentPosition() on the IPhone seems to be due to the way in which the IPhone resolves it’s position, (initially at least) by GSM triangulation, which becomes progressively more accurate as more readings are taken. Thus, navigator.geolocation.watchPosition() allows the IPhone to progressively improve the accuracy of it’s Geo location reading whereas navigator.geolocation.getCurrentPosition() will return only the initial Geo location reading which in my experiments is almost always the position of the GSM tower to which your GSM connection is bound – pretty much useless in most circumstances.

It also seems that the IPhone will not engage it’s Satelite-based Geo location services in Safari, thus we have to make do with GSM triangulation positioning. This typically resolves my position to an accuracy of 15 to 20 metres in my experiments.

So…in summary, to obtain accurate Geo location positioning on an IPhone in Safari, use navigator.geolocation.watchPosition() rather than navigator.geolocation.getCurrentPosition(), simple!

I have a working example of navigator.geolocation.watchPosition() which works on IPhone for your viewing pleasure!

Created: Thu, 27 May 2010 08:00:00 GMT
Last modified: Thu, 27 May 2010 08:00:00 GMT