如何匹配Google地图距离矩阵中的目的地

时间:2015-07-27 11:40:43

标签: javascript google-maps-api-3

我有一个网络应用程序,需要“与我的距离”信息显示在地图上的属性。

我正在使用谷歌距离矩阵服务

distanceService = new google.maps.DistanceMatrixService();

我正在使用一个来源和多个目的地来呼叫该服务。

  var params = {
    origins: [currentLocation],
    destinations: endPoints,
    travelMode: google.maps.TravelMode.DRIVING
  }
  distanceService.getDistanceMatrix(params, saveDistances);

我的endPoints是从我的属性中取出的google.maps.LatLng个对象的数组。但是,当返回结果时,他们已将这些更改为地址,并且不再引用LatLng

有人知道距离矩阵服务是否保证按照我发送的顺序返回结果?我在docs中看不到任何内容。我真的不想开始调用地理编码服务来匹配我的属性(特别是因为LatLng的返回不是完全匹配)

2 个答案:

答案 0 :(得分:3)

它们按照发送的顺序返回。

这没有具体说明,但我的阅读方式the documentation暗示了它。

quick test fiddle

代码段

var service = new google.maps.DistanceMatrixService();
var places = [{
  address: "New York, NY",
  lat: 40.7143528,
  lng: -74.0059731
}, {
  address: "Tampa, FL",
  lat: 27.950575,
  lng: -82.4571776
}, {
  address: "Newark, NJ",
  lat: 40.735657,
  lng: -74.1723667
}, {
  address: "Boston, MA",
  lat: 42.3584308,
  lng: -71.0597732
}, {
  address: "Baltimore, MD",
  lat: 39.2903848,
  lng: -76.6121893
}];
var foundlatlng = new google.maps.LatLng(40.65, -73.95); // Brooklyn, NY
var gotoLoc = [];
for (var i = 0; i < places.length; i++) {
  gotoLoc.push(new google.maps.LatLng(places[i].lat, places[i].lng));
}
var service = new google.maps.DistanceMatrixService(); //request distance matrix
var outputdiv = document.getElementById('info');

//     var goto = new google.maps.LatLng(places[i].lat, places[i].lng);
function callback(response, status) {
    var distancefield = distancefield;
    if (status == google.maps.DistanceMatrixStatus.OK) {
      var origins = response.originAddresses;
      var destinations = response.destinationAddresses;
      var htmlString = "<table border='1'>";
      var bounds = new google.maps.LatLngBounds();
      for (var c = 0; c < response.rows.length; c++) {
        var results = response.rows[c].elements;
        for (var r = 0; r < results.length; r++) {
          var element = results[r];
          var distancetext = element.distance.text;
          var durationtext = element.duration.text;
          var to = destinations[r];
          htmlString += "<tr><td>" + (r + 1) + "</td><td>" + places[r].address + "</td><td>" + response.originAddresses[c] + "</td><td>" + to + "</td><td>" + distancetext + "</td><td>" + durationtext + "</td></tr>";
          var marker = new google.maps.Marker({
            icon: 'http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/marker' + (r + 1) + '.png',
            position: {
              lat: places[r].lat,
              lng: places[r].lng
            },
            map: map
          });
          bounds.extend(marker.getPosition());
        } //end for r
      } // end for c
      map.fitBounds(bounds);
      htmlString += "</table>";
      document.getElementById('info').innerHTML = htmlString;


    } //end if status=ok
  } //end callback

//    console.log(places);

var map;
var geocoder = new google.maps.Geocoder();

function initialize() {
  var mapOptions = {
    zoom: 8,
    center: foundlatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'),
    mapOptions);

  service.getDistanceMatrix({
    origins: ["San Diego, CA", foundlatlng],
    destinations: gotoLoc,
    travelMode: google.maps.TravelMode.WALKING,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    avoidHighways: false,
    avoidTolls: false,
  }, callback); //end service.getdistancematrix()


}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%
}
#map_canvas {
  height: 400px;
  width: 100%;
}
tr,
td,
tbody,
table {
  height: 100%;
  width: 100%;
}
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<table border="1">
  <tr>
    <td>
      <div id="map_canvas"></div>
    </td>
  </tr>
  <tr>
    <td>
      <div id="info"></div>
    </td>
  </tr>
</table>

答案 1 :(得分:1)

如果您能为API响应中返回的每个Origin和Destination提供索引/ ID,那么它会更有帮助吗?

如果保证订单与发送的完全相同,我同意这应该可以正常工作。但这并没有保证,并且它似乎随意限制了谷歌以更适合他们的方式订购结果的能力。