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:
- void navigator.geolocation.getCurrentPosition(success_callback_function, error_callback_function, position_options)
- long navigator.geolocation.watchPosition(success_callback_function, error_callback_function, position_options)
- void navigator.geolocation.clearWatch(watch_position_id)
position_options is specified as a JSON-style string with up to three parameters:
- enableHighAccuracy – A boolean (true/false) which indicates to the device that you wish to obtain it’s most accurate readings (this parameter may or may not make a difference, depending on your hardware)
- maximumAge – The maximum age (in milliseconds) of the reading (this is appropriate as the device may cache readings to save power and/or bandwidth)
- timeout – The maximum time (in milliseconds) for which you are prepared to allow the device to try to obtain a Geo location
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:
- coords.latitude – The current latitude reading
- coords.longitude – The current longitude reading
- coords.accuracy – The accuracy of the current latitude and longitude readings (in metres)
- coords.speed – The current speed reading in metres per second (you can simply multiply by 2.2369 to convert to miles per hour or multiply by 3.6 to convert to kilometres per hour)
- coords.altitude – The current altitude reading (in metres)
- coords.altitudeAccuracy – The accuracy of the current altitude reading (in metres)
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.