最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS Geolocation wait until success before return value - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Nov 23, 2013 at 18:15 GusDeCooLGusDeCooL 5,75818 gold badges70 silver badges103 bronze badges 6
  • it does wait for the correct position, but it's asynchronous, not only that, but you have a five second delay before you write the cookie that you yourself created with a timeout ? – adeneo Commented Nov 23, 2013 at 18:24
  • yes, i tried use timeout to make sure the browser get the position. but still it return undefined. – GusDeCooL Commented Nov 23, 2013 at 18:30
  • Once again, it's asynchronous, and you can't return a value from an async function like that. – adeneo Commented Nov 23, 2013 at 18:32
  • Forget the timeout, you need a callback. – Andy Commented Nov 23, 2013 at 18:32
  • @Andy Can you please give example how to write callback for it? – GusDeCooL Commented Nov 23, 2013 at 18:34
 |  Show 1 more comment

1 Answer 1

Reset to default 17

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);
      }
    )
}
发布评论

评论列表(0)

  1. 暂无评论