Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Geolocation wait until success before return value

I tried developing browser geolocation, but it seems geolocation quickly return a value when it is still searching for my location.

Example of my script:

function updateCoordinate() {
        navigator.geolocation.getCurrentPosition(
                function (position) {
                    setTimeout(function() {
                        var returnValue = {
                            latitude: position.coords.latitude,
                            longitude: position.coords.longitude
                        }
                        var serializeCookie = serialize(returnValue);
                        $.cookie('geolocation', serializeCookie);
                        return serializeCookie;
                    }, 5000);
                },
                function () {
                    alert('Sorry, we are failed to get your location')
                }, {timeout: 5000}
        )
    }

If we execute this script updateCoordinate, the function will return undefined. But after a moment if we check the cookie it set right the coordinate.

How to make getCurrentPosition waiting until get exact coordinate before returning the value?

like image 948
GusDeCooL Avatar asked Nov 23 '13 18:11

GusDeCooL


People also ask

What does getCurrentPosition () in Geolocation API returns?

Geolocation getCurrentPosition() API 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.

What does showPosition () in Geolocation API returns?

What does showPosition() returns? Explanation: showPosition() method returns both latitude and longitude of user.

What is getCurrentPosition return?

The getCurrentPosition() method returns the current position of the device.

How do you use watchPosition?

The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.


1 Answers

Use a callback, not a timeout which will end you up in all sorts of problems. Something along the lines of:

// Here you pass a callback function as a parameter to `updateCoordinate`.
updateCoordinate(function (cookie) {
  console.log(cookie);
});

function updateCoordinate(callback) {
    navigator.geolocation.getCurrentPosition(
      function (position) {
        var returnValue = {
          latitude: position.coords.latitude,
          longitude: position.coords.longitude
        }
        var serializeCookie = serialize(returnValue);
        $.cookie('geolocation', serializeCookie);

        // and here you call the callback with whatever
        // data you need to return as a parameter.
        callback(serializeCookie);
      }
    )
}
like image 163
Andy Avatar answered Oct 19 '22 23:10

Andy