-1

我有以下代码用于来自 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);
    }


    });

    }

谢谢

4

1 回答 1

0

这不是一个数字:

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

这可以给我以公里为单位的总距离:

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

工作示例

于 2013-06-25T23:51:09.003 回答