2011-04-13 49 views
2

我不使用JavaScript太多,所以我不熟悉回調和響應。我使用谷歌地圖來繪製A點和X,Y,Z點之間的距離。值得注意的是,我想用javascript來確定哪個點X,Y,Z最接近A,並且它們會映射它們之間的方向。等待循環完成之前在javascript中傳遞值(也是谷歌地圖)。

我的代碼正在工作。我可以計算出所有3個目的地的最短距離,但我被這個愚蠢的目光困住了。

您會發現,Google使用異步回調向瀏覽器提供數據,如果我運行for循環來逐個檢查所有3個目標,我會得到不正確的結果。

下面是代碼:

var maxDistance = 99999999999; 
var destn; 
for (var i = 0; i < locations.length; i++) { 
    var thisDistance = 0; 
    var start = origin; 
    var end = locations[i]; 
    var request = { 
    origin: start, 
    destination: end, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 
    directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     thisDistance = response.routes[0].legs[0].distance.value; 
     if (thisDistance < maxDistance) { 
     destn = response.routes[0].legs[0].end_address; 
     maxDistance = thisDistance; 
     } 
    } else { 
     document.getElementById("addressNotFound").style.display = 'block'; 
    } 
    }); 
} 
calcShortestRoute(origin, destn); 

所以,很顯然,當我調用這個函數,紙位置的值出現,因爲循環結束與谷歌處理程序處理不當接收到的數據卻是不確定的。如果我再次調用函數1,我會得到從前一個回調(之前未定義)接收到的destn值。

有人請告訴我如何解決這個問題。

回答

4

您需要等到所有三個Google回覆都已返回。一個簡單的解決方案是:將你距離計算函數調用到末尾的匿名函數,然後calc下的距離只有當所有響應都返回:

 // global count variable 
     var callbackCount = 0; 

     for (var i = 0; i<locations.length; i++) { 
      var thisDistance=0; 
      var start = origin; 
      var end = locations[i]; 
      var request = { 
       origin:start, 
       destination:end, 
       travelMode: google.maps.DirectionsTravelMode.DRIVING 
      }; 
      directionsService.route(request, function(response, status) { 
       if (status == google.maps.DirectionsStatus.OK) { 
        thisDistance = response.routes[0].legs[0].distance.value; 
        if (thisDistance < maxDistance) { 
         destn = response.routes[0].legs[0].end_address; 
         maxDistance = thisDistance; 
        }        
       } 
       else { 
        document.getElementById("addressNotFound").style.display = 'block'; 
       } 

       // move this to here, and pass in number of locations 
       calcShortestRoute(origin, destn, locations.length); 
      }); 



     } 

然後calcShortestRoute樣子:

function calcShortestRoute(origin, destn, locationCount) { 

    // increment callback count 
    callbackCount++; 

    if (callbackCount == locationCount) { // all responses have returned 


    // do your distance checking  
    .... 

    } 
} 
+0

' calcShortestRoute''''''''''''''''''''''''''''將始終通過''''完成',因爲'''將永遠是'請參閱http://jsfiddle.net/gilly3/9526m進行簡單演示。即使它按照你想要的那樣工作,它仍然無法工作。異步調用可以按任意順序返回。所以,僅僅因爲最後的請求已經返回,並不意味着所有的請求都返回了。 – gilly3 2011-04-13 22:26:06

+0

@ gilly3 - 是的,當然,迴應可能不會返回順序:)我已經修改我的回答 – 2011-04-14 11:46:26

+1

真棒。這工作得很好。謝謝Rich。 – 2011-04-14 14:24:43

4

當你退出的循環,因爲它被設置異步directionsService.route()收到您的結果後,您不會有destn值。相反,你需要跟蹤的多少請求返回,並調用你的函數從響應回調中,你已經收到所有的答覆之後:

... // your code 
var responseCount = 0; 
for (var i = 0; i < locations.length; i++) { 
    ... // your code 
    directionsService.route(request, function(response, status) { 
     ... // your code 
     if (++responseCount == locations.length) { 
      calcShortestRoute(origin, destn); 
     } 
    }); 
} 

編輯:我重讀你的問題,並認爲我更好地理解你的代碼在做什麼。這個答案應該更準確(並且更加簡潔,可以啓動!)。

相關問題