<small id='K9dEA'></small><noframes id='K9dEA'>

<i id='K9dEA'><tr id='K9dEA'><dt id='K9dEA'><q id='K9dEA'><span id='K9dEA'><b id='K9dEA'><form id='K9dEA'><ins id='K9dEA'></ins><ul id='K9dEA'></ul><sub id='K9dEA'></sub></form><legend id='K9dEA'></legend><bdo id='K9dEA'><pre id='K9dEA'><center id='K9dEA'></center></pre></bdo></b><th id='K9dEA'></th></span></q></dt></tr></i><div id='K9dEA'><tfoot id='K9dEA'></tfoot><dl id='K9dEA'><fieldset id='K9dEA'></fieldset></dl></div>

    <legend id='K9dEA'><style id='K9dEA'><dir id='K9dEA'><q id='K9dEA'></q></dir></style></legend>

      <tfoot id='K9dEA'></tfoot>
          <bdo id='K9dEA'></bdo><ul id='K9dEA'></ul>

        JS Geolocation 等到成功再返回值

        时间:2023-09-06
        <legend id='4WZ6E'><style id='4WZ6E'><dir id='4WZ6E'><q id='4WZ6E'></q></dir></style></legend>

            • <bdo id='4WZ6E'></bdo><ul id='4WZ6E'></ul>
              <i id='4WZ6E'><tr id='4WZ6E'><dt id='4WZ6E'><q id='4WZ6E'><span id='4WZ6E'><b id='4WZ6E'><form id='4WZ6E'><ins id='4WZ6E'></ins><ul id='4WZ6E'></ul><sub id='4WZ6E'></sub></form><legend id='4WZ6E'></legend><bdo id='4WZ6E'><pre id='4WZ6E'><center id='4WZ6E'></center></pre></bdo></b><th id='4WZ6E'></th></span></q></dt></tr></i><div id='4WZ6E'><tfoot id='4WZ6E'></tfoot><dl id='4WZ6E'><fieldset id='4WZ6E'></fieldset></dl></div>
                    <tbody id='4WZ6E'></tbody>

                  <small id='4WZ6E'></small><noframes id='4WZ6E'>

                  <tfoot id='4WZ6E'></tfoot>
                  本文介绍了JS Geolocation 等到成功再返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我尝试开发浏览器地理定位,但似乎地理定位在仍在搜索我的位置时会很快返回一个值.

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

                  我的脚本示例:

                  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}
                          )
                      }
                  

                  如果我们执行这个脚本updateCoordinate,函数会返回undefined.但过了一会儿,如果我们检查 cookie,它会正确设置坐标.

                  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.

                  如何让getCurrentPosition等到获得精确坐标后再返回值?

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

                  推荐答案

                  使用回调,而不是超时,这会让你遇到各种各样的问题.大致如下:

                  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);
                        }
                      )
                  }
                  

                  这篇关于JS Geolocation 等到成功再返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:geolocation.getCurrentPosition 如何返回值? 下一篇:谷歌地图 - 当前位置

                  相关文章

                  最新文章

                  <i id='KtJ0w'><tr id='KtJ0w'><dt id='KtJ0w'><q id='KtJ0w'><span id='KtJ0w'><b id='KtJ0w'><form id='KtJ0w'><ins id='KtJ0w'></ins><ul id='KtJ0w'></ul><sub id='KtJ0w'></sub></form><legend id='KtJ0w'></legend><bdo id='KtJ0w'><pre id='KtJ0w'><center id='KtJ0w'></center></pre></bdo></b><th id='KtJ0w'></th></span></q></dt></tr></i><div id='KtJ0w'><tfoot id='KtJ0w'></tfoot><dl id='KtJ0w'><fieldset id='KtJ0w'></fieldset></dl></div>
                1. <small id='KtJ0w'></small><noframes id='KtJ0w'>

                  1. <tfoot id='KtJ0w'></tfoot>

                        <bdo id='KtJ0w'></bdo><ul id='KtJ0w'></ul>
                    1. <legend id='KtJ0w'><style id='KtJ0w'><dir id='KtJ0w'><q id='KtJ0w'></q></dir></style></legend>