【问题标题】:how to calculate Distance between two coordinates using Mapbox Kotlin如何使用 Mapbox Kotlin 计算两个坐标之间的距离
【发布时间】: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) {

                }
            })
    }

【问题讨论】:

    标签: android kotlin mapbox


    【解决方案1】:

    而不是这个:

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

    这样做:

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

    【讨论】:

    • 因为这个答案是正确的,所以我会接受它作为答案,我只是很久以前就知道了,但是感谢任何方式都比未回答的问题更好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2020-02-12
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多