Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait to reach an accuracy threshold with getCurrentPosition

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...

like image 711
Ian Grainger Avatar asked Jun 07 '11 12:06

Ian Grainger


People also ask

Which choice is the mandatory parameter of the getCurrentPosition () method?

Which choice is the mandatory parameter of the getCurrentPosition () method? Explanation: Coordinates of object is return by getCurrentPosition() method. getCurrentPosition() function accepts three parameters i.e. success, position and error. When data is fetched successfully success callback will be invoked.

What is the significance of the first parameter in getCurrentPosition () API?

The error callback function, if provided when calling getCurrentPosition() or watchPosition() , expects a GeolocationPositionError object instance as its first parameter.

Which function is used to get the correct position using geolocation API?

The Geolocation. getCurrentPosition() method is used to get the current position of the device.

What is navigator geolocation getCurrentPosition?

Description. The getCurrentPosition method retrieves the current geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed. The location information is returned in a Position object.


1 Answers

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();
like image 193
ThinkingStiff Avatar answered Sep 22 '22 15:09

ThinkingStiff