我有一个 Android 应用程序,其中包含应按特定顺序遵循的 POI 坐标列表。 显然,如果应用程序/GPS 在第一个之前打开,那就很容易了。但是在两点中间打开应用程序时如何处理?
它们是步道/徒步旅行点,因此目前不需要街道,我们可以将它们视为空间中的点。
是否有任何 Android 库(离线)可以提供帮助,或者我需要编写自己的代码? 我期待这样的事情:
您还有其他建议吗?
谢谢!
以下是如何在 Kotlin 中实现此功能的简化示例:
import android.location.Location
data class POI(val name: String, val latitude: Double, val longitude: Double)
fun findClosestConsecutivePOIs(currentLocation: Location, poiList: List<POI>): Pair<POI, POI>? {
if (poiList.size < 2) return null
var closestPair: Pair<POI, POI>? = null
var shortestDistance = Double.MAX_VALUE
for (i in 0 until poiList.size - 1) {
val poiA = poiList[i]
val poiB = poiList[i + 1]
// Get distances
val distanceA = currentLocation.distanceTo(Location("").apply {
latitude = poiA.latitude
longitude = poiA.longitude
})
val distanceB = currentLocation.distanceTo(Location("").apply {
latitude = poiB.latitude
longitude = poiB.longitude
})
// Check forward direction
if (distanceA < shortestDistance && distanceB > distanceA) {
closestPair = Pair(poiA, poiB)
shortestDistance = distanceA
}
}
return closestPair
}