我需要确定一个人的位置在50米以内。 我想知道我是否应该使用
以下是我的代码:
navigator.location.watchPosition()
或反复调用 getCurrentPosition()
。 watchPosition()
是我想要做的正确的 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()
时,页面加载需要一段时间。