执行doInBackground()并在Kotlin中将航点添加到Mapbox时发生错误

时间:2019-10-16 14:35:15

标签: android kotlin mapbox mapbox-android

我在Android MapBox应用程序中使用了Kotlin,直到我尝试向使用MapBox Navigation SDK生成的路线中添加多个Waypoint为止,一切正常。

这是我使用的代码:

        val finalWayPointsList = mutableListOf<Point>()
        finalWayPointsList.add(waypoints[0])
        finalWayPointsList.add(waypoints[1])
        println("the wayPoint list is $finalWayPointsList")

        val builder = NavigationRoute.builder(this)
            .accessToken(getString(R.string.access_token))
            .origin(origin)
            .destination(destination)
        for (wayPoint in finalWayPointsList) {
            builder.addWaypoint(wayPoint)
        }
        println("ended loop")
        builder.build()
            .getRoute(object : retrofit2.Callback<DirectionsResponse> {
                override fun onResponse(
                    call: retrofit2.Call<DirectionsResponse>,
                    response: retrofit2.Response<DirectionsResponse>
                ) {
                    println("got response")
                    val body = response.body() //?: return
                    if (body?.routes()?.count() == 0) {
                        println("Main Activity Error : No route found")
                        return
                    }
                    if (navigationMapRoute != null) {
                        println("reached navigationMapRoute != null ")
                        navigationMapRoute?.removeRoute()
                    } else {

                        println("reached else ")
                        navigationMapRoute =
                            NavigationMapRoute(null, mapView!!, mapboxMap!!)
                    }
                    navigationMapRoute?.addRoute(body?.routes()?.first())
                }

                override fun onFailure(
                    call: retrofit2.Call<DirectionsResponse>,
                    t: Throwable
                ) {

                }
            })
        mapboxMap!!.animateCamera(
            CameraUpdateFactory.newCameraPosition(
                CameraPosition.Builder()
                    .target(
                        LatLng(
                            fromLat, fromLong
                        )
                    )
                    .zoom(14.0)
                    .build()
            ), 2000
        )

添加多个WayPoint失败,尽管当我使用此代码块仅添加1个Waypoint时效果很好:

        val builder = NavigationRoute.builder(this)
            .accessToken(getString(R.string.access_token))
            .origin(origin)
            .destination(destination)
            .addWaypoint(Waypoints[0])
        builder.build()

但是最后我得到了错误:

An error occurred while executing doInBackground()

0 个答案:

没有答案