好了,所以我有一些,抓住用戶的地理位置,因爲這樣用戶可以移動它是設置到一個定時的setTimeout重新運行的地理定位功能,每3秒其中,如果他們要搬家會展示自己的進步,但是我想它,如果他們沒有移動,例如它們的緯度和經度相同,則沒有更新,因爲沒有任何理由了。更新地理位置每隔*秒,但不是在拉/長並沒有改變
我的思路是沿着如下:
function startGeolocation() {
var options;
navigator.geolocation.getCurrentPosition(geoSuccess, geoFail, options);
}
//get various coordinates
function geoSuccess(position) {
var gpsPosition = position;
var coordinates = gpsPosition.coords;
//if its the first run through and myLat is empty, then continue as normal.
if (!myLat){
myLat = coordinates.latitude;
myLong = coordinates.longitude;
//if its the second run through and the myLat is the same as it was before, do nothing.
}else if ((myLat == myLatSame) && (myLong == myLongSame)){
}
//else if they are different, e.g user has moved, then update.
else{
myLat = coordinates.latitude;
myLong = coordinates.longitude;
setTimeout(geoSuccess, 3000);
}
myLatSame = myLat;
myLongSame = myLong;
}
它似乎不工作,並在頁面完全停止加載地圖。
然而如果我回去非常基本的代碼,
function startGeolocation() {
var options;
navigator.geolocation.getCurrentPosition(geoSuccess, geoFail, options);
setTimeout(startGeolocation, 3000);
}
function geoSuccess(position) {
var gpsPosition = position;
var coordinates = gpsPosition.coords;
myLat = coordinates.latitude;
myLong = coordinates.longitude;
這個工作正常,並每3秒更新。
我特地從一個漫長的沉寂重新編碼的javascript,所以我的語法和方法是有點生疏。 由於事先
編輯:
我已經通過下面增加了幾個警報代碼,並在第一次運行情況: 其第一次運行通過這樣的警報(前如果)=未定義myLat和myLong。因爲它擁有什麼使myLat和myLong得到充滿COORDS,並通知在警報(在IF),以下警告(myLatSame + myLongSame)回來爲「南」
的否則,如果不 !myLat是真實的觸發的是不一樣的,但是 的else警報(否則)語句也不會被觸發,並沒有看到。
//get various coordinates
function geoSuccess(position) {
var gpsPosition = position;
var coordinates = gpsPosition.coords;
alert("before if \n" + myLat + "\n" + myLong);
//if its the first run through and myLat is empty, then continue as normal.
if (!myLat){
myLat = coordinates.latitude;
myLong = coordinates.longitude;
alert("in if \n" + myLat + "\n" + myLong);
alert(myLatSame + myLongSame);
//if its the second run through and the myLat is the same as it was before, do nothing.
}else if ((myLat == myLatSame) && (myLong == myLongSame)){
alert("in else if \n" + myLat + "\n" + myLong);
}
//else if they are different, e.g user has moved, then update.
{
myLat = coordinates.latitude;
myLong = coordinates.longitude;
alert("else \n" + myLat + "\n" + myLong);
setTimeout(geoSuccess, 3000);
}
myLatSame = myLat;
myLongSame = myLong;
}
還有呢?有什麼問題?它工作嗎?有錯誤嗎?問題在哪裏? – jbabey 2013-02-26 19:38:09
對不起,在其他地方有偏差。該代碼不起作用,並且地圖現在根本不加載。 – user2112613 2013-02-26 19:44:29
錯誤訊息? – 2013-02-26 19:45:58