如何找到 Google 地图 API 最短路线,如 Google 地图路线所示

How to find Google Map API Shortest route as Shown in Google map Route

我在获取最短路线时遇到了问题。 问题是 google 地图中显示的距离与 google.maps.DirectionsService() 获得的最短路线之间存在差异;我也得到了替代路线,但仍然与地图上显示的距离不匹配.. enter image description here

地图中显示的距离为 15 公里,但 google.maps.DirectionsService() 的最短路线为 18.3 公里 我想获得地图中显示的距离,如我所附的图像所示。 代码...

       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;
           }
       });
   }

您的代码实现中的其他地方可能存在您尚未发布的问题,因为您共享的代码 returns 与 Google 地图的距离相同。

请看一下这个jsfiddle based off of Google's example进行演示。请按照以下步骤操作:
1. Select "Frankfurt Airport (FRA) (FRA), Frankfurt, Germany" 作为原点
2. Select "Frankfurt Marriott Hotel, Hamburger Allee, Frankfurt, Germany" 作为目的地
3. Select "Driving" 模式

控制台输出:

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

相关代码如下:

  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);
    }
  });

希望对您有所帮助!