<legend id='2kvna'><style id='2kvna'><dir id='2kvna'><q id='2kvna'></q></dir></style></legend>

      1. <tfoot id='2kvna'></tfoot>
          <bdo id='2kvna'></bdo><ul id='2kvna'></ul>

      2. <small id='2kvna'></small><noframes id='2kvna'>

      3. <i id='2kvna'><tr id='2kvna'><dt id='2kvna'><q id='2kvna'><span id='2kvna'><b id='2kvna'><form id='2kvna'><ins id='2kvna'></ins><ul id='2kvna'></ul><sub id='2kvna'></sub></form><legend id='2kvna'></legend><bdo id='2kvna'><pre id='2kvna'><center id='2kvna'></center></pre></bdo></b><th id='2kvna'></th></span></q></dt></tr></i><div id='2kvna'><tfoot id='2kvna'></tfoot><dl id='2kvna'><fieldset id='2kvna'></fieldset></dl></div>
      4. geolocation.getCurrentPosition 如何返回值?

        时间:2023-09-06
          <bdo id='ILrb6'></bdo><ul id='ILrb6'></ul>

            <tfoot id='ILrb6'></tfoot>

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

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

                1. 本文介绍了geolocation.getCurrentPosition 如何返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 getCurrentPosition 来获取当地的纬度和经度.我知道这个函数是异步的,只是想知道如何返回其他函数可以访问的经纬度值?

                  I am using getCurrentPosition to get local latitude and longitude. I know this function is asynchronous, just wondering how to return latitude and longitude value that can be accessed by other functions?

                      function getGeo() {
                          navigator.geolocation.getCurrentPosition(getCurrentLoc)
                      }
                  
                      function getCurrentLoc(data) {
                          var lat,lon;
                          lat=data.coords.latitude;
                          lon=data.coords.longitude;
                          getLocalWeather(lat,lon)
                          initMap(lat,lon)
                      }
                  

                  推荐答案

                  我建议你把它包装在一个承诺中:

                  I would suggest you wrapping it in a promise:

                  function getPosition() {
                      // Simple wrapper
                      return new Promise((res, rej) => {
                          navigator.geolocation.getCurrentPosition(res, rej);
                      });
                  }
                  
                  async function main() {
                      var position = await getPosition();  // wait for getPosition to complete
                      console.log(position);
                  }
                  
                  main();
                  

                  https://jsfiddle.net/DerekL/zr8L57sL/

                  ES6 版本:

                  function getPosition() {
                      // Simple wrapper
                      return new Promise((res, rej) => {
                          navigator.geolocation.getCurrentPosition(res, rej);
                      });
                  }
                  
                  function main() {
                      getPosition().then(console.log); // wait for getPosition to complete
                  }
                  
                  main();
                  

                  https://jsfiddle.net/DerekL/90129LoL/

                  这篇关于geolocation.getCurrentPosition 如何返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Google 地图中模拟点击 下一篇:JS Geolocation 等到成功再返回值

                  相关文章

                  最新文章

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

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

                  • <bdo id='GgjNt'></bdo><ul id='GgjNt'></ul>

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