5

Looking at the web GeoLocation API documentation there are two ways to get location - getCurrentPosition to get a quick reading of location and watchPosition to monitor the changes to the position.

What I'm looking for is a way to get a location reading which is quite accurate, as quickly as possible. I think the ideal would be to use an accuracy threshold on the getCurrentPosition call - and the success handler would be called when the threshold is reached or the timeout exceeded (with as accurate as possible a result).

This doesn't already exist, right? Presumably it would be fairly straightforward to implement this by just wrapping the watchPosition method to stop checking after a timeout or the threshold is reached. I will use this approach (and intend to post the code) if no-one is aware of a built in method to do this...

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72

2 Answers2

3

No built in function. I've found that a timeout is better than a set accuracy. Often times on an iPhone the accuracy will be no better than around 100 meters, so continuing to check for better is wasteful. Using watchPosition() has worked best and it seems to triangulate, so after three readings it rarely gets better. I just wait five seconds and stop.

Demo: http://jsfiddle.net/ThinkingStiff/3k859/

HTML:

<div id="result"></div>

Script:

function setGeolocation() {

    var geolocation = window.navigator.geolocation.watchPosition( function ( position ) {

            document.getElementById( 'result' ).innerHTML += 
                  'lat: ' + position.coords.latitude + ', '
                + 'lng: ' + position.coords.longitude + ', '
                + 'accuracy: ' + position.coords.accuracy + '<br />';

        },
        function () {
            //error
        },
        {
            maximumAge: 250, 
            enableHighAccuracy: true
        } 

    );

    window.setTimeout( function () {

        window.navigator.geolocation.clearWatch( geolocation ) 

    }, 5000 );

};

setGeolocation();
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
  • Thanks for showing the function. This is what I planned to write, but getCurrentPosition seemed fine for my uses. – Ian Grainger Nov 10 '11 at 11:40
  • I've marked this as the answer, because it's pretty much what I wanted originally - but I'm not planning to use it, so have no idea whether it works! – Ian Grainger Nov 11 '11 at 09:07
2

Based on my experience developing J2ME geo-location based apps for Taxi, GPS chip take some initialization to get accurate location reading. The preferred method is to use method similar to watchPosition(), which will keep the GPS chip hot to provide accurate location reading.

I think your approach is sound in theory, but it needs to be tested on real device as GPS chips varied in performance across devices.

Hope this helps.

GeorgeW
  • 566
  • 5
  • 17
  • Question: Is there a function which gets location to a certain level of accuracy automatically (or times out). Answer: No. Fair summary? :) – Ian Grainger Jul 05 '11 at 16:35