使用Mapbox跟踪用户在Android中旅行的距离

时间:2019-06-02 08:40:50

标签: android mapbox mapbox-android

在使用mapbox的Android中是否有任何API可以跟踪/测量用户的行进距离?

Direction Matrix API在这种情况下有用吗?我已经使用Google Direction APIs实现了它,但是如果可能的话,我想切换到Mapbox

1 个答案:

答案 0 :(得分:0)

没关系。我通过执行以下操作得到了解决方法:

private fun calculateDirections(currentLocation: Point) {
    activity?.let { context ->
        MapboxDirections.builder()
            .origin(currentLocation)
            .destination(Point.fromLngLat(model.longitude, model.latitude))
            .overview(DirectionsCriteria.OVERVIEW_FULL)
            .profile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC)
            .accessToken(Mapbox.getAccessToken() ?: "")
            .build()
            .enqueueCall(object : Callback<DirectionsResponse>{
                override fun onFailure(call: Call<DirectionsResponse>, t: Throwable) {

                }

                override fun onResponse(
                    call: Call<DirectionsResponse>,
                    response: Response<DirectionsResponse>
                ) {
                    if(response.body() != null) {
                        val distance = response.body()!!.routes()[0].distance() ?: 0.0
                        val time = response.body()!!.routes()[0].duration() ?: 0.0
                        Toast.makeText(context,String.format("Distance: %.2f km Time: %.2f min",distance/1000.0, time/24), Toast.LENGTH_LONG).show()
                    }
                }
            })


    }
}