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

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

        watchPosition() 与 getCurrentPosition() 与 setTimeout

        时间:2023-09-02

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

                1. <small id='tZDtx'></small><noframes id='tZDtx'>

                  <legend id='tZDtx'><style id='tZDtx'><dir id='tZDtx'><q id='tZDtx'></q></dir></style></legend><tfoot id='tZDtx'></tfoot>
                2. 本文介绍了watchPosition() 与 getCurrentPosition() 与 setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要确定一个人在 50m 内的位置.我想知道我应该使用 navigator.location.watchPostion() 还是一遍又一遍地调用 getCurrentPostion() .watchPostion() 是正确的 W3C API 来做我想做的事,但实际上,它似乎有点矫枉过正.

                  I need to determine a person's location within 50m. I'm wondering if I should use navigator.location.watchPostion() or call getCurrentPostion() over and over again. watchPostion() is the proper W3C API for doing what I want, but practically, it seems to be overkill.

                  这是我的代码:

                  var map = null;
                  var marker = null;
                  var layer = null;
                  
                  function locFn(pos) {
                  
                    var lat = pos.coords.latitude;
                    var lon = pos.coords.longitude;
                  
                    $("#hlat").val(lat);
                    $("#hlong").val(lon);
                  
                    document.getElementById("lnkMap").href = 
                      "http://maps.google.com/maps?q=My+Loc@" + lat
                      + "," + lon + "&z=18&t=h";
                  
                    var point = new GLatLng(lat, lon);
                  
                    if (pos.coords.accuracy < 100) {
                      map.setCenter(point, 18);
                  
                      if (marker != null) {
                        marker.setLatLng(point);
                      }
                      else {
                        var ico = new GIcon();
                        ico.image = "img/Blue-Dot.png";
                        ico.iconSize = new GSize(22, 22);
                        ico.iconAnchor = new GPoint(11, 11);
                        marker = new GMarker(point, { icon: ico });
                        layer = map.addOverlay(marker, { title: "You are here." });
                      }
                    }
                    else if (pos.coords.accuracy > 2000) {
                      if (marker != null) { marker.setLatLng(point); }
                      map.setCenter(point, 15);
                    }
                    else if (pos.coords.accuracy > 900) {
                      if (marker != null) { marker.setLatLng(point); }
                      map.setCenter(point, 16);
                    }
                    else if (pos.coords.accuracy > 100) {
                      if (marker != null) { marker.setLatLng(point); }
                      map.setCenter(point, 17);
                    }
                  }
                  
                  function locFail() {
                    //alert("Failed to retrieve location.");
                  }
                  
                  var watchID = null;
                  
                  function processPoints() {
                    map = new GMap2(document.getElementById("map_canvas"), 
                                    { mapTypes: [G_HYBRID_MAP] });
                    try {
                      watchID = navigator.geolocation.watchPosition(locFn, locFail,
                            { enableHighAccuracy: true });
                    }
                    catch(err) { /* desktop?*/ }
                  }
                  $(function(){processPoints();});
                  

                  我注意到 watchPostion() 似乎最终会导致更高的准确性(一段时间后),但我想知道位置变化是否如此之快以至于导致很多事情下载到我的地图画布,不断的 http 请求很快就会过时,被新的请求所取代.当我使用 watchPosition() 时,页面加载需要一段时间.

                  I've noticed watchPostion() seems to ultimately result in more accuracy (after a while), but I'm wondering if the position changes so fast that it results in a lot of thing being downloaded to my map canvas, with constant http requests that are soon out-of-date, replaced by the new ones coming in. When I use watchPosition(), it does take a while before the page loads.

                  推荐答案

                  经过一些认真的测试,我已经验证了 watchPosition() 会比 getCurrentPostion() 一遍又一遍地更快地为您提供准确的位置.使用 watchPostion() 时,如果您在每次设备更新您的位置时一遍又一遍地重新绘制地图,则地图的行为会很差.为了解决这个问题,我在 tilesloaded 事件中添加了一个侦听器,它允许我仅在没有线程尝试在地图上绘制时才重绘地图.一旦用户对确定的位置感到满意,我将清除手表.就电池消耗和准确性而言,这将使我两全其美.

                  After some serious testing, I have verified watchPosition() will give you an accurate location much more quickly than getCurrentPostion() over and over again. When using watchPostion(), the map behaves poorly if you redraw it over and over again every time the device updates your location. To get around this, I have added a listener to the tilesloaded event, which allows me to only redraw the map if there is not already a thread trying to draw on the map. Once the user is happy with the determined location, I will clear the watch. This will get me the best of both worlds, as far as battery consumption and accuracy are concerned.

                  这篇关于watchPosition() 与 getCurrentPosition() 与 setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在Android中根据与当前位置的距离对地理点进 下一篇:如果我已经有 ACCESS_FINE_LOCATION,我可以省略 ACC

                  相关文章

                  最新文章

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

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