問題描述
我需要確定一個(gè)人在 50m 內(nèi)的位置.我想知道我應(yīng)該使用 navigator.location.watchPostion()
還是一遍又一遍地調(diào)用 getCurrentPostion()
.watchPostion()
是正確的 W3C API 來做我想做的事,但實(shí)際上,它似乎有點(diǎn)矯枉過正.
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()
似乎最終會(huì)導(dǎo)致更高的準(zhǔn)確性(一段時(shí)間后),但我想知道位置變化是否如此之快以至于導(dǎo)致很多事情下載到我的地圖畫布,不斷的 http 請求很快就會(huì)過時(shí),被新的請求所取代.當(dāng)我使用 watchPosition()
時(shí),頁面加載需要一段時(shí)間.
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.
推薦答案
經(jīng)過一些認(rèn)真的測試,我已經(jīng)驗(yàn)證了 watchPosition() 會(huì)比 getCurrentPostion() 一遍又一遍地更快地為您提供準(zhǔn)確的位置.使用 watchPostion() 時(shí),如果您在每次設(shè)備更新您的位置時(shí)一遍又一遍地重新繪制地圖,則地圖的行為會(huì)很差.為了解決這個(gè)問題,我在 tilesloaded 事件中添加了一個(gè)偵聽器,它允許我僅在沒有線程嘗試在地圖上繪制時(shí)才重繪地圖.一旦用戶對(duì)確定的位置感到滿意,我將清除手表.就電池消耗和準(zhǔn)確性而言,這將使我兩全其美.
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.
這篇關(guān)于watchPosition() 與 getCurrentPosition() 與 setTimeout的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!