У меня есть проблемы.
здесь я запускаю функцию обратного вызова с функцией getRoute (), чтобы получить маршрут для навигации
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
Mapbox.getInstance(this.context!!, getString(R.string.token_mapbox))
mapView = view.findViewById(R.id.mapView)
mapView?.onCreate(savedInstanceState)
Log.d("test", "test1")
val autocomplete : GeocoderAutoCompleteView = view.findViewById(R.id.address_search)
autocomplete.setAccessToken(Mapbox.getAccessToken())
autocomplete.setType(GeocodingCriteria.TYPE_POI)
autocomplete.setCountry("CA")
autocomplete.setOnFeatureListener {
fun onFeatureClick(feature : CarmenFeature) {
hideOnScreenKeyboard()
val position : Position = feature.asPosition()
updateMap(position.latitude, position.longitude)
}
}
mapView?.getMapAsync { mapboxMap ->
map = mapboxMap
Log.d("test", "test4")
enableLocationPlugin()
Log.d("test", "test3")
originCoord = LatLng(originLocation?.latitude!!, originLocation?.longitude!!)
Log.d("test", "test")
Log.d("test", "test8")
startButton.setOnClickListener(View.OnClickListener {v ->
Log.d("test","enter here")
val mapboxGeocoding : MapboxGeocoding = MapboxGeocoding.builder().accessToken(Mapbox.getAccessToken()!!).query(address_search.text.toString()).build()
Log.d("test","1")
if (destinationMarker != null) {
mapboxMap.removeMarker(destinationMarker!!)
}
Log.d("test","2")
mapboxGeocoding.enqueueCall(object : Callback<GeocodingResponse>
{
override fun onResponse(call : Call<GeocodingResponse>, response : Response<GeocodingResponse>) {
Log.d("test","3")
val results = response.body()!!.features()
if (results.size > 0) {
// Log the first results Point.
val firstResultPoint : Point = results[0].center()!!
Log.d("test", "onResponse: " + firstResultPoint.toString());
val test = LatLng(firstResultPoint.latitude(), firstResultPoint.longitude())
destinationCoord = test
destinationMarker = mapboxMap.addMarker(MarkerOptions().position(destinationCoord))
destinationPosition = Point.fromLngLat(destinationCoord!!.longitude, destinationCoord!!.latitude)
originPosition = Point.fromLngLat(originCoord!!.longitude, originCoord!!.latitude)
getRoute(originPosition!!, destinationPosition!!)
startButton.isEnabled = true
startButton.isClickable = true
var simulateRoute: Boolean = true
var options: NavigationLauncherOptions = NavigationLauncherOptions.builder().directionsRoute(currentRoute).shouldSimulateRoute(simulateRoute).build()
NavigationLauncher.startNavigation(activity, options)
}
else {
// No result for your request were found.
Log.d(TAG, "onResponse: No result found");
}
}
override fun onFailure(call : Call<GeocodingResponse>, throwable: Throwable) {
throwable.printStackTrace()
}
})
})
}
}
Но когда я хочу использовать переменную currentroute, которая установлена в функции getRoute в NavigationLauncherOptions, он говорит мне, что переменная равна нулю.
Так что я думаю, что обратный вызов не заканчивается и функция Navagation запускается до его завершения.
вот функция getRoute
private fun getRoute(origin : Point, destination : Point) {
Log.d("test", "testgetroute")
NavigationRoute.builder(this.context)
.accessToken(Mapbox.getAccessToken()!!)
.origin(origin)
.destination(destination)
.build()
.getRoute( object: Callback<DirectionsResponse> {
override fun onResponse(call : Call<DirectionsResponse>, response : Response<DirectionsResponse>) {
// You can get the generic HTTP info about the response
Log.d("test", "Response code: " + response.code())
if (response.body() == null) {
Log.e("test", "No routes found, make sure you set the right user and access token.")
return
} else if (response.body()!!.routes().size < 1) {
Log.e("test", "No routes found")
return
}
currentRoute = response.body()!!.routes()[0]
Log.d("test", currentRoute.toString() + " current route is set")
// Draw the route on the map
if (navigationMapRoute != null) {
navigationMapRoute!!.removeRoute()
} else {
navigationMapRoute = NavigationMapRoute(null, mapView!!, map!!, R.style.NavigationMapRoute)
}
navigationMapRoute!!.addRoute(currentRoute)
}
override fun onFailure(call : Call<DirectionsResponse>, throwable: Throwable) {
Log.e("test", "Error: " + throwable.message)
}
})
}
Я много ищу, и я новичок в kotlin, но кто-то понял проблему?
потому что я не могу найти решения
Спасибо за ваш ответ и ваше время
08-31 00:15:58.965 25377-25377/com.example.parky.parky_android D/test: enter here
08-31 00:15:58.971 25377-25377/com.example.parky.parky_android D/test: 1
2
08-31 00:15:59.129 25377-25377/com.example.parky.parky_android D/test: 3
08-31 00:15:59.131 25377-25377/com.example.parky.parky_android D/test: onResponse: Point{type=Point, bbox=null, coordinates=[-71.269566, 46.779652]}
08-31 00:15:59.179 25377-25377/com.example.parky.parky_android D/test: testgetroute
08-31 00:15:59.230 25377-25377/com.example.parky.parky_android D/AndroidRuntime: Shutting down VM
08-31 00:15:59.235 25377-25377/com.example.parky.parky_android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.parky.parky_android, PID: 25377
java.lang.NullPointerException: Null directionsRoute
at com.mapbox.services.android.navigation.ui.v5.AutoValue_NavigationLauncherOptions$Builder.directionsRoute(AutoValue_NavigationLauncherOptions.java:151)
at ItemFourFragment$onViewCreated$2$1$1.onResponse(ItemFourFragment.kt:320)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
08-31 00:15:59.454 25377-25382/com.example.parky.parky_android I/art: Compiler allocated 8MB to compile retrofit2.ParameterHandler retrofit2.ServiceMethod$Builder.parseParameterAnnotation(int, java.lang.reflect.Type, java.lang.annotation.Annotation[], java.lang.annotation.Annotation)