1

I have various origins/destinations, and am looping through all of them to calculate each distance. Pushing each to an array of the following object

routes { 
    distance: xx,
    response:response
     }

After all the routes have been added I calculate which has the smallest distance, and map that response. However I am outside the directionService now.. is there a way to map stored responses without calling directionService?

Thanks!!

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}); 
 }
 });
}
brdu
  • 284
  • 1
  • 3
  • 17
  • 1
    I had a similar problem and a found my solution here: http://stackoverflow.com/questions/19320146/array-of-destinations-with-google-direction-api#answer-19364370 – Diogo Cid Nov 12 '14 at 11:08

0 Answers0