How to calculate total distance and time (getDistanceMatrix)

后端 未结 1 1276
庸人自扰
庸人自扰 2021-01-29 07:35

I need to get total distance and travel time with service.getDistanceMatrix({, sum A + B + C + D = total distance.

The way that i try, only i get the first

1条回答
  •  -上瘾入骨i
    2021-01-29 08:23

    The DirectionsService returns all the information you need to calculate the total time and distance, you don't need to use the DistanceMatrix.

    Per this related question: Google Maps API: Total distance with waypoints

    function computeTotalDistance(result) {
      var totalDist = 0;
      var totalTime = 0;
      var myroute = result.routes[0];
      for (i = 0; i < myroute.legs.length; i++) {
        totalDist += myroute.legs[i].distance.value;
        totalTime += myroute.legs[i].duration.value;
      }
      totalDist = totalDist / 1000.
      document.getElementById("dvDistance").innerHTML = "total distance is: " + totalDist + " km
    total time is: " + (totalTime / 60).toFixed(2) + " minutes"; }

    proof of concept fiddle

    code snippet:

    var source, destination;
    var directionsService = new google.maps.DirectionsService();
    var stop;
    var markers = [];
    var waypts = [];
    
    stop = new google.maps.LatLng(6.347033, -75.559017)
    waypts.push({
      location: stop,
      stopover: true
    });
    stop = new google.maps.LatLng(6.348764, -75.562970)
    waypts.push({
      location: stop,
      stopover: true
    });
    var directionsDisplay = new google.maps.DirectionsRenderer({
      suppressMarkers: false,
    });
    
    window.onload = function() {
      var mapOptions = {
        zoom: 15,
        //center: new google.maps.LatLng(6.3490548, -75.55802080000001),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
      }
      var map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
      directionsDisplay.setMap(map);
      directionsDisplay.setPanel(document.getElementById('dvPanel'));
    
      source = new google.maps.LatLng(6.3490548, -75.55802080000001);
      destination = new google.maps.LatLng(6.334624, -75.556157);
    
      var request = {
        origin: source,
        destination: destination,
        waypoints: waypts,
        optimizeWaypoints: true,
        travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
          computeTotalDistance(response);
        }
      });
    }
    
    function computeTotalDistance(result) {
      var totalDist = 0;
      var totalTime = 0;
      var myroute = result.routes[0];
      for (i = 0; i < myroute.legs.length; i++) {
        totalDist += myroute.legs[i].distance.value;
        totalTime += myroute.legs[i].duration.value;
      }
      totalDist = totalDist / 1000.
      document.getElementById("dvDistance").innerHTML = "total distance is: " + totalDist + " km
    total time is: " + (totalTime / 60).toFixed(2) + " minutes"; }
    html,
    body {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px;
    }
    
    #dvMap {
      height: 100%;
      width: 50%;
      margin: 0px;
      padding: 0px;
    }
    
    #dvPanel {
      height: 100%;
      width: 50%;
      margin: 0px;
      padding: 0px;
      float: right;
    }
    
    

    0 讨论(0)
提交回复
热议问题