如何使用 Mapbox Kotlin 计算两个坐标之间的距离
how to calculate Distance between two coordinates using 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)
但问题是上面的这个方法它不计算路线内的距离,它只是计算点到点的直线例如 在Google地图上距离是9Km,但是用上面这个方法距离是6Km
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()
如何从 Mapbox 导航路线中获取距离?在双格式中,我不想绘制路线我只需要计算两点之间的距离,我已经能够使用 Truf
计算距离Storelocation = Point.fromLngLat(Stores.latitude, Stores.longitude)
Userlocation = Point.fromLngLat(UserLocation.Latitude, UserLocation.Longitude)
Distance = TurfMeasurement.distance(Storelocation, Userlocation, TurfConstants.UNIT_KILOMETERS)
但问题是上面的这个方法它不计算路线内的距离,它只是计算点到点的直线例如 在Google地图上距离是9Km,但是用上面这个方法距离是6Km
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()