在对象数组中存储directionsService.route响应,并在另一个方法中显示

时间:2013-11-21 08:44:56

标签: javascript google-maps google-maps-api-3

我有各种各样的起源/目的地,并且循环遍历所有这些来计算每个距离。将每个推送到以下对象的数组

routes { 
    distance: xx,
    response:response
     }

添加完所有路线后,我计算出距离最小的路线,然后映射该路线。但是我现在在directionService之外..有没有办法在不调用directionService的情况下映射存储的响应?

谢谢!

var routes = [];

function route() {
  var request = {
  origin:start,
  destination:end,
  travelMode: google.maps.TravelMode.DRIVING
  };
  calcRoute(request);

  //Here I'd like to display, say routes[0].response.

}


function calcRoute() {
  var start = document.getElementById("start").value;
  var end = document.getElementById("end").value;

directionsService.route(request, function(response, status) {
 if (status == google.maps.DirectionsStatus.OK) {
  var route = response.routes[0];
  // For each route, display summary information.
  var distance = 0;
  for (var i = 0; i < route.legs.length; i++) {
    distance = distance + route.legs[i].distance.text;
  };
  //push to routes
  routes.push({response:response,distance:distance}); 
 }
 });
}

0 个答案:

没有答案