【发布时间】:2021-04-27 19:09:28
【问题描述】:
如何获取 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) {
}
})
}
【问题讨论】: