2012-10-22 131 views
0

我有代碼來計算距離地點到另一個地方使用谷歌距離矩陣API,但免費其每個查詢有限的只有100個元素。因此,我使用循環代碼(例如元素)來製作代碼以解決問題,我的代碼是PHP,JavaScript,並且我從MySql中解析了緯度和經度數據。任何人都知道我的代碼在下面的錯誤?因爲如果我點擊計算按鈕它不會出現任何內容。 Actualy我modifie從這個URL https://google-developers.appspot.com/maps/documentation/javascript/examples/distance-matrix的代碼,這是我的代碼:谷歌距離矩陣API 3

function hitung(){ 
    service = new google.maps.DistanceMatrixService(); 
    for (m = 0 ; m < l-80 ; m++){ 
     asal = originarray[m]; 
     for (n = 0 ; n < l-80 ; n++){ 
      tujuan = destarray [n]; 
      calculateDistances(); 
     } 
    } 
    } 
    function calculateDistances() { 

//var s = 0; 

    //for (m = 0 ; m < l-80 ; m++){ 
    // asal = originarray[m]; 
    //for (n = 0 ; n < l-80 ; n++){ 
     //servicearray[s] = new google.maps.DistanceMatrixService(); 
     //tujuan = destarray [n];  
     service.getDistanceMatrix(
     { 
     //origins: [origin1, origin2], 
     // destinations: [destinationA, destinationB], 
     origins: asal, 
    destinations: tujuan, 
     //origins: originarray, 
     //destinations: destarray, 
     travelMode: google.maps.TravelMode.DRIVING, 
     unitSystem: google.maps.UnitSystem.METRIC, 
     avoidHighways: false, 
     avoidTolls: false 
     }, callback); 
    // s++; 

    //} 

// }   
    } 

    function callback(response, status) { 
    if (status != google.maps.DistanceMatrixStatus.OK) { 
     alert('Error was: ' + status); 
    } else { 
     var origins = response.originAddresses; 
     var destinations = response.destinationAddresses; 
     var outputDiv = document.getElementById('outputDiv'); 
     //outputDiv.innerHTML = ''; 
    // deleteOverlays(); 

     for (var i = 0; i < origins.length; i++) { 
     var results = response.rows[i].elements; 
     // addMarker(origins[i], false); 
     for (var j = 0; j < results.length; j++) { 
     // addMarker(destinations[j], true); 
      outputDiv.innerHTML += origins[i] + ' to ' + destinations[j] 
       + ': ' + results[j].distance.text + ' in ' 
       + results[j].duration.text + '<br>'; 
     } 
     } 
    } 
    } 

    function addMarker(location, isDestination) { 
    var icon; 
    if (isDestination) { 
     icon = destinationIcon; 
    } else { 
     icon = originIcon; 
    } 
    geocoder.geocode({'address': location}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     bounds.extend(results[0].geometry.location); 
     map.fitBounds(bounds); 
     var marker = new google.maps.Marker({ 
      map: peta, 
      position: results[0].geometry.location, 
      icon: icon 
     }); 
     markersArray.push(marker); 
     } else { 
     alert('Geocode was not successful for the following reason: ' 
      + status); 
     } 
    }); 
    } 

    function deleteOverlays() { 
    if (markersArray) { 
     for (i in markersArray) { 
     markersArray[i].setMap(null); 
     } 
     markersArray.length = 0; 
    } 
    } 

</script> 

+0

我有同樣的問題,但我認爲問題出在你的數組(tujuan和asal)。我尋找解決這個問題,但我沒有找到答案。如果你找到答案,你可以在這裏發送?如果我找到了,我也一樣。 – user2141655

回答

0

檢查,如果你登錄的響應接收來自谷歌的迴應......

function callback(response, status) { 
    console.log(response); 
}