如何在现有折线上计算LatLng A到LatLng B?

问题描述

我正在使用与Waze或Google Maps类似的Android应用。我已经有一个由Direction API填充的折线

现在我需要根据生成的折线来计算从标记A到标记B的距离

enter image description here

我假设我可以通过循环折线坐标来计算距离,但是标记位于折线坐标的中间。正如您在底部看到的距离,即标记A和标记B之间的直线距离不正确。

有人知道如何根据生成的折线计算标记A与标记B之间的距离吗?

任何帮助都会很棒!谢谢!请问您有什么问题

解决方法

从生成从Directions API获取的折线的坐标列表中,获取标记A坐标的索引以及标记B坐标的索引,然后从标记A的索引开始并以的索引结束市场B。使用下面的gradle代码下载Google Maps utils库。

dependencies {

  // KTX for the Maps SDK for Android library
  implementation 'com.google.maps.android:maps-ktx:2.1.3'

  // KTX for the Maps SDK for Android Utility Library
  implementation 'com.google.maps.android:maps-utils-ktx:2.1.3'

  // It is recommended to also include the latest Maps SDK and/or Utility Library versions
  // as well to ensure that you have the latest features and bug fixes.
  implementation 'com.google.android.gms:play-services-maps:17.0.0'
  implementation 'com.google.maps.android:android-maps-utils:2.0.3'
}

然后您可以使用从上述库的PolyUtil类中获得的以下方法来获取标记点A和B的索引:

 Polyutil.locationIndexOnEdgeOrPath(LatLng point,java.util.List<LatLng> poly,boolean closed,boolean geodesic,double toleranceEarth)

使用以下选项:

  1. 关闭(假)
  2. 大地(假)
  3. toleranceEarth(0.01)