Improving geolocation getCurrentPosition() with getAccurateCurrentPosition()
getAccurateCurrentPosition() is a simple enhancement to navigator.geolocation that provides a more accurate and predictable result. It is intended for any geolocation-enabled web browser. This is also usable in PhoneGap applications since PhoneGap uses the underlying HTML geolocation APIs already. I have tested this on desktop Chrome, Safari, Firefox and on iOS and Android devices. I have not tested on IE9+ or Opera or Windows devices.
See the code, docs, and sample usage at https://github.com/gwilson/getAccurateCurrentPosition





Thank you! I had noticed this exact behavior in my apps (Phonegap apps on iOS), and wanted to set up my code to ignore the “last checked position” if it was old, but couldn’t figure it out. It looks like you solved it. Thanks!
Greg: nice, much more graceful than how I was doing it! Here’s my thoughts on HTML5 Geolocation accuracy in general: http://www.andygup.net/?p=600. And, hey, if you are at OSCON next week I’m presenting on HTML5 Geolocation.
Diggin’ this, Greg – nicely done. What is the maximumAge option for in your example though? I didn’t see it used anywhere else. Thanks!
Nevermind Greg. After a night of research (it’s 5.10am), I have discovered that there is a problem in iOS6 where if your web app has the tag, geolocation breaks. Have had to remove it on the page that uses geolocation, so it’s not as pretty (no full screen), but it works.
So the problem was not with my current geolocation code after all. Cheers.
Hi Greg the function you provided here getAccurateCurrentPosition is not working in my application.
This is my code below:
function getCurrentPosition()
{
navigator.geolocation.getAccurateCurrentPosition(geoLocSuccess, geoLocError, {desiredAccuracy:20, maxWait:15000});
//navigator.geolocation.getCurrentPosition(geoLocSuccess, geoLocError, { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true });
}
function geoLocSuccess(position)
{
currentPosition = position;
}
But when I was using getCurentPosition it was working perfectly fine.
Please help. Thanks in advance.
Any errors?
Here’s the latest code — https://github.com/gwilson/getAccurateCurrentPosition
I haven’t tested in a while, so maybe something broke with latest OS updates? Let me know what error you are seeing.
Greg
I am not getting the accurate position. I think the location is the network tower’s location within 1 km range. This method is returning the same location when I used getCurrentPosition.
Please help.