TheDotProduct

Web-tech nerd stuff

A simple example of navigator.geolocation.watchPosition()

As I mentioned in my previous post about the new/upcoming navigator.geolocation standard, I have been experimenting with Geo location on my IPhone so I thought I’d share some of that with you in case it helps anyone out.

My example is very simple and is intended to show how you can use the JavaScript navigator.geolocation.watchPosition() (the more accurate method of Geo location on IPhone) to determine your Geo location.

So, without further ado, here it is, my example of navigator.geolocation.watchPosition(). The JavaScript code is commented but if you have any questions, please post them as comments and I’ll do my best to answer them. I’ll be honest here too, i’ve not really tested the script on anything other than my IPhone so if it doesn’t work for you, please let me know!

The navigator.geolocation object offers is quite small but offers the following (at the time of writing):

Methods:

position_options is specified as a JSON-style string with up to three parameters:

For example, your options could specified as per my example:

wpid=navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});

The success_callback_function is passed a single parameter, a position object which has the following properties:

So, for example if your success_callback_function is specified as in my example:

wpid=navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});

the success_callback_function would be called “geo_success” and could be as follows:

function geo_success(position)
{
    document.title=position.coords.speed;
}

Which would simply change the document.title to the current speed reading.

You can find the full specification for the navigator.geolocation interface here: http://dev.w3.org/geo/api/spec-source.html#geolocation_interface.

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