0
我正在爲移動用戶開發一個WebApp,它已經去預定義的位置,並且首先可以看到有一個<div>
(文本,圖片或其他)。 所以,我有一個預定義的位置和當前的用戶位置。如果兩個地點都相同,我會展示一些東西。 我該怎麼辦?這個代碼不工作:地理位置watchPosition。當位置到達時如何獲得一個事件?
var id, ziel, options;
//Verfolgen beginnen
id = navigator.geolocation.watchCurrentPosition(verfolgePosition);
function verfolgePosition(pos) {
var aktuell = pos.coords;
if (ziel.latitude === aktuell.latitude && ziel.longitude === aktuell.longitude) {
console.log('Sie haben Ihr Ziel erreicht');
//Verfolgen beenden
navigator.geolocation.clearWatch(id);
}
}
ziel = {
latitude : 0,
longitude: 0
};
options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0
};
的方式,目標可以是:
ziel = {
latitude : 52.514971,
longitude: 13.369172
};
沒有方法'watchCurrentPosition()'。你正在尋找的是'.watchPosition()'[W3](https://dev.w3.org/geo/api/spec-source.html#watch-position)/ [mdn](https:// developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition) – Andreas
Yes is true。我將它改爲'.watchPosition',但它只能使用一次。也許是因爲可移動位置(移動設備)快速變化以獲得確切的固定位置,同樣,如果我在相同位置測試此腳本。我該如何解決它? – kubus1234
從我的評論中的w3鏈接:「_在手錶過程的步驟5.2.2中,只有在獲得新位置並且此**位置與先前報告的位置**顯着不同時才調用successCallback **。關於什麼構成顯着差異的定義留待執行。**此外,在步驟5.2.2和5.2.3中,實現可能對回調頻率施加限制,以避免不經意地消耗不成比例的資源** ._「 – Andreas