【问题标题】:watchPosition() vs getCurrentPosition() with setTimeoutwatchPosition() 与 getCurrentPosition() 与 setTimeout
【发布时间】:2010-12-29 06:27:43
【问题描述】:

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

这是我的代码:

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() 时,页面加载确实需要一段时间。

【问题讨论】:

  • 有什么方法可以发布您的解决方案的完整 HTML+Javascript 示例吗?包括监听器和 enableContinuousZoom() 调用?我对 GMap2 不熟悉,所以我不得不进行逆向工程......但它可能会在某天为我的其他人节省一些工作......
  • 查看我在下面回复 Sam 的评论。
  • 查看文档似乎在 GetCurrentPosition (尽快运行成功处理程序)和 WatchPosition (设计为在用户在页面上的整个时间内运行)的极端之间缺少一些东西。我本来希望在 GetCurrentPosition 调用上有一个“准确度阈值”选项——它等待这个准确度或超时。除非其他人已经使用 WatchPosition 创建了这个,我想我现在就这样做。

标签: javascript iphone google-maps geolocation iphone-web-app


【解决方案1】:

每次调用 getCurrentLocation 时,gps 都会启动,这需要一些时间。 如果您调用 watchPosition 并等到您获得一定的准确性,然后移除您的手表,您将获得更好的结果,而不会产生持续手表的开销。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 2012-05-02
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多