0

我有代码可以使用谷歌距离矩阵 API 计算到另一个地方的距离,但每个查询免费限制只有 100 个元素。所以我使用循环代码(如 for elements)编写代码来解决问题,我的代码是 PHP、javascript,我从 MySql 解析纬度和经度数据。有人知道我下面的代码有什么错误吗?因为如果我单击计算按钮,它不会出现任何内容。实际上我已经从这个 URL https://google-developers.appspot.com/maps/documentation/javascript/examples/distance-matrix修改了代码,这是我的代码:

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>

4

1 回答 1

0

通过记录响应来检查您是否收到来自 Google 的响应...

function callback(response, status) {
    console.log(response);
}
于 2013-02-28T15:29:00.480 回答