谷歌地图v3中所有航点的总距离和时间

时间:2013-06-25 19:38:13

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

我有以下来自google maps apiv3的路线代码。这部分工作得很好。如果我在旅​​行中有航点,在每次旅行的顶部,它显示旅行的时间和距离。 我使用了一个变量totadistance来增加所有腿的距离,但它什么也没做。我运行应用程序时没有看到警告消息。 我想看看所有旅行的总时间和距离。我怎样才能获得这些信息?

  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) {
  directionsDisplay.setDirections(response);
  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 ;

          }
       alert(totaldistance);
    }


    });

    }

谢谢

1 个答案:

答案 0 :(得分:0)

这不是数字:

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

这可以给我一个以km为单位的总距离:

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

working example