Google 地图 API 计算起点和终点之间的准确距离

Google Maps API to calculate exact distance between origin and destination

我想尽可能计算出准确的距离,但目前我只是添加起点和终点,让 Google 方向 API 计算它,但这并不意味着它会正确的路线,所以我想知道是否可以使用编码折线来计算距离矩阵或其他东西的距离?

Call<Direction> call = RESTClient.getGoogleApiService().getDirectionsWithLatLng(startLatLng, endLatLng);

...

@GET("/maps/api/directions/json")
Call<Direction> getDirectionsWithLatLng(
        @Query(value = "origin") String position,
        @Query(value = "destination") String destination);

有什么不同:

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au&waypoints=enc:lexeF{~wsZejrPjtye@:

Google Maps Distance Matrix API returns 信息基于起点和终点之间的推荐路线,由 Google 地图 API 计算得出。所以这里的结果只是根据Google计算出的距离。此外,此服务没有 return 详细的路线信息。可以通过将所需的单一起点和终点传递给 Google 地图方向 API.

来获取路线信息

Directions API 不同,您可以使用 waypoints 设置您想要计算的方式,距离矩阵 API 只需计算最佳方式或正确方式的距离设置起点和终点的方式。

For your question about the two JSON request.

第一个请求return没有waypoints的响应,所以基本上它就像距离矩阵API一样return它的信息基于推荐起点和终点之间的路线,由 Google 地图 API

计算得出

第二个请求是 waypoints,因此当您观察到结果有更多公里或更长的结果时,因为您指定了 API 需要计算的 waypoints。

有关更多信息,我尝试同时使用方向 API 和距离矩阵 API。

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au

-

https://maps.googleapis.com/maps/api/distancematrix/json?origins=sydney+au&destinations=perth+au

如您所见,如果您没有在说明 API 中使用 waypoints。 Directions API 结果与 Distance Matrix API.

相同(值有微小差异,但以公里为单位的文本相同)