谷歌距离矩阵API 3

时间:2012-10-22 08:49:13

标签: matrix distance

我有使用谷歌距离矩阵API计算到另一个地方的距离的代码,但是免费的每个查询仅限100个元素。所以我使用循环代码(例如元素)来编写解决问题的代码,我的代码是PHP,javascript,并且我解析来自MySql的纬度和经度数据。有人知道我的代码有些错误吗?因为如果单击“计算”按钮,它就不会显示任何内容。 Actualy我修改了这个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>

1 个答案:

答案 0 :(得分:0)

通过记录回复来检查您是否收到了Google的回复...

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