Improving geolocation getCurrentPosition() with getAccurateCurrentPosition()

June 30, 2012 | By

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

Filed in: Android, HTML5, iOS/iPhone/iPad, JavaScript, PhoneGap | Tags: , , , , , , ,

About the Author (Author Profile)

Greg is a developer advocate for the Google Cloud Platform based in San Francisco, California
  • http://spatialityapps.com Steven Romalewski

    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!

  • http://blog.andygup.net Andy Gup

    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.

  • Trae Regan

    Diggin’ this, Greg – nicely done. What is the maximumAge option for in your example though? I didn’t see it used anywhere else. Thanks!

  • http://www.songbookdb.com Shaun Thomson

    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.

    • Shahabaz Khan

      Could you please be specific on what you meant by ‘tag’? Because I am also not able to get results with this.

      • shi11

        I believe he meant plugin, ie. if you have the phonegap geolocation plugin, this breaks. Whereas if you let Phonegap use the html5 built-in geolocation (without the plugin) then it will work. At least that’s what I’m experiencing on ios7.

  • Nidhi Dagar

    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.

  • Nidhi Dagar

    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.

  • Paulo Roberto Martins de Andra

    Can you help me? I try use this, but I`m receiving the error: Uncaught TypeError: Object [object Object] has no method ‘getAccurateCurrentPosition’