如何使用Mapbox Kotlin计算两个坐标之间的距离



如何获取Mapbox导航路线的距离?在双格式中,我不想画路线,我只需要计算两点之间的距离,我已经能够使用Truf 计算距离

Storelocation = Point.fromLngLat(Stores.latitude, Stores.longitude)
Userlocation = Point.fromLngLat(UserLocation.Latitude, UserLocation.Longitude)
Distance = TurfMeasurement.distance(Storelocation, Userlocation, TurfConstants.UNIT_KILOMETERS)

但问题是,上面的方法不计算路线内的距离,它只是计算从点到点的直线,例如在谷歌地图中,距离是9公里,但上面的方法的距离是6公里

private fun getRout(){
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken()!!)
.origin(Userlocation)
.destination(Storelocation).build().getRoute(object :Callback<DirectionsResponse> {
override fun onResponse(call: Call<DirectionsResponse>, response: Response<DirectionsResponse>) {
val rout = response ?: return
val body = rout.body() ?: return
if (body.routes().count() == 0 ){
return
}
navigationMapRoute = NavigationMapRoute(null, mapView, map)
// Get Distance
Distance = ??
}
override fun onFailure(call: Call<DirectionsResponse>, t: Throwable) {
}
})
}

而不是这个:

navigationMapRoute = NavigationMapRoute(null, mapView, map)
// Get Distance
Distance = ??

这样做:

val route = response.body().routes().get(0)
val distance = route.distance()

最新更新