total distance and time for all waypoints in google maps v3

I have the following code for directions from google maps apiv3. This part is working good. If i have waypoints in my trip, at the top of each trip, it is showing the time as well as distance for the trip. I used a variable totadistance to add the distance from all the legs but, it does nothing. I do not see a alert message when i run the application. I want to see the total time and distance for all the trips. How can i get that information?

  function calcRoute(startaddr, endaddr) {

  var start = document.getElementById(startaddr).value;
  var end = document.getElementById(endaddr).value;
  var waypts = [];
  var waypointstring;
  var waypoint1 = document.getElementById('txtWaypoint').value; 

  waypointstring= waypoint1.split(";");
 //alert("Waypoint Length:" + waypointstring.length)

  for (var i = 0; i < waypointstring.length; i++) {

     waypts.push({location:waypointstring[i], stopover:true});

  var request = {
  origin: start, 
  destination: end,
  waypoints: waypts,
  optimizeWaypoints: true,
  travelMode: google.maps.TravelMode.DRIVING  };
  var totaldistance=0;
    directionsService.route(request, function(response, status) {

  if (status == google.maps.DirectionsStatus.OK) {
  var route = response.routes[0];
  var summaryPanel = document.getElementById('directions_panel');

  summaryPanel.innerHTML = "";

  // For each route, display summary information.
  for (var i = 0; i < route.legs.length; i++) {
    var routeSegment = i + 1;
    summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
    summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
    summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
    summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
     totaldistance = totaldistance + route.legs[i].distance.text ;




Thank you



This is not a number:

totaldistance = totaldistance + route.legs[i].distance.text;

This works to give me the total distance in km:

 totaldistance = totaldistance + route.legs[i].distance.value;

working example


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.