-2

I am getting trouble while getting the Shortest route . The Problem is that there is difference between the distance shown in google map and shortest routes get by the google.maps.DirectionsService(); I am also getting alternative roues but that still not match with distance show on map .. enter image description here

The Distance shown in Map is 15-km but the shortest routes by google.maps.DirectionsService() is 18.3-Km I want to get the distance shown in map as in image i attach. code...

       directionsService.route(request, function(response, status) {
           if (status === 'OK') {
               let route_options = [];
               for (var i = 0; i < response.routes.length; i++)
               {
                   let route = response.routes[i];
                   let distance = 0;
                   // Total the legs to find the overall journey distance for each route option
                   for (var j = 0; j < route.legs.length; j++)
                   {
                       distance += route.legs[j].distance.value; // metres
                   }
                   route_options.push({
                       'route_id': i,
                       'distance': distance
                   });
               }
               route_options.sort(function(a, b) {
                   return parseInt(a.distance) - parseInt(b.distance);
               });
               let duration = response.routes[0].legs[0].duration.value;
               let shortest_distance = (route_options[0]['distance'] * 0.001); // convert metres to kilometres


              return shortest_distance;
           }
       });
   }
evan
  • 5,443
  • 2
  • 11
  • 20

1 Answers1

0

There's likely an issue somewhere else in your code implementation that you haven't posted, because the code you've shared returns the same distance that Google Maps does.

Please take a look at this jsfiddle based off of Google's example for demonstration. Follow these steps:
1. Select "Frankfurt Airport (FRA) (FRA), Frankfurt, Germany" as origin
2. Select "Frankfurt Marriott Hotel, Hamburger Allee, Frankfurt, Germany" as destination
3. Select "Driving" mode

Console output:

distance.text: 15.0 km
distance.value: 14.988
shortest_distance: 14.988

Relevant code below:

  function(response, status) {
    if (status === 'OK') {
      let route_options = [];
      for (var i = 0; i < response.routes.length; i++) {
        let route = response.routes[i];
        let distance = 0;
        // Total the legs to find the overall journey distance for each route option
        for (var j = 0; j < route.legs.length; j++) {
          distance += route.legs[j].distance.value; // metres
        }
        route_options.push({
          'route_id': i,
          'distance': distance
        });
      }
      route_options.sort(function(a, b) {
        return parseInt(a.distance) - parseInt(b.distance);
      });
      let duration = response.routes[0].legs[0].duration.value;
      let shortest_distance = (route_options[0]['distance'] * 0.001); // convert metres to kilometres

      console.log("distance.text: " + response.routes[0].legs[0].distance.text);
      console.log("distance.value: " + response.routes[0].legs[0].distance.value * 0.001);
      console.log("shortest_distance: " + shortest_distance);

      me.directionsRenderer.setDirections(response);
    } else {
      window.alert('Directions request failed due to ' + status);
    }
  });

Hope this helps!

evan
  • 5,443
  • 2
  • 11
  • 20