2013-07-19 34 views
0

所以這是我的問題。 回調函數未被調用。我不知道爲什麼。或者甚至這是如何工作的。 如何使用參數調用函數,而不是將參數放在調用中?Google Maps API v3此回調函數如何工作?

這裏的

var addr = new Array(5); 
    addr[0] = new google.maps.LatLng(-27.352646,-53.384881); 
    addr[1] = new google.maps.LatLng(-27.344648,-53.395009); 
    addr[2] = new google.maps.LatLng(-27.365562,-53.388859); 
    addr[3] = new google.maps.LatLng(-27.366241,-53.401655); 
    addr[4] = new google.maps.LatLng(-27.360467,-53.397476); 


    var a = new google.maps.LatLng(-27.352901,-53.402745); 
    var menorDistancia; 
    var destinoFinal; 
    function callback(response, status) { 
     alert("CHEGOU AQUI") 
     if (status == google.maps.DistanceMatrixStatus.OK) { 
     var origins = response.originAddresses; 
     var destinations = response.destinationAddresses; 

    for (var i = 0; i < origins.length; i++) { 
     var results = response.rows[i].elements; 
     for (var j = 0; j < results.length; j++) { 
     var element = results[j]; 
     var distance = element.distance.text; 
     var duration = element.duration.text; 
     var from = origins[i]; 
     var to = destinations[j]; 
     if(distance < menorDistancia || i==0){ 
      menorDistancia = distance; 
      destinoFinal = to; 
     } 
     } 
    } 
    } 
    } 

    var service = new google.maps.DistanceMatrixService(); 
    service.getDistanceMatrix(
     { 
      origins: [a,a,a,a,a], 
      destinations: [addr[0],addr[1],addr[2],addr[3],addr[4]], 
     }, callback); 
+0

您的腳本看起來沒問題,控制檯中是否有任何錯誤? –

回答

1

你缺少travelMode代碼:

the documentation
service.getDistanceMatrix(
    { 
     origins: [a,a,a,a,a], 
     destinations: [addr[0],addr[1],addr[2],addr[3],addr[4]], 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }, callback); 

travelMode | TravelMode |請求的路由類型。 必需

+0

TravelMode是可選的,如果省略,默認爲DiretionsTravelMode.DRIVING。 https://developers.google.com/maps/documentation/javascript/distancematrix –

+1

哈,剛剛看到您的編輯,但我的鏈接仍然表示它是可選的默認值:) –

+1

文檔不一致。當它不在那裏的代碼扔了一個JavaScript錯誤,它沒有被定義,它沒有工作。將它添加到請求工作(回調運行)。 – geocodezip