我正在遵循Mapbox的Android Route Generation教程,并且使用了与文档完全相同的代码:
val origin = Point.fromLngLat(124.132465, -35.752721)
val destination = Point.fromLngLat(125.132465, -36.752721)
NavigationRoute.builder(context)
.accessToken(getString(R.string.mapbox_access_token))
.origin(origin)
.destination(destination)
.build()
.getRoute(object : Callback<DirectionsResponse> {
override fun onFailure(call: Call<DirectionsResponse>, t: Throwable) {
Toast.makeText(context, "Failed to get directions", Toast.LENGTH_SHORT).show()
Log.d(TAG, "Failed to get directions")}
override fun onResponse(call: Call<DirectionsResponse>, response: Response<DirectionsResponse>) {
Toast.makeText(context, "Successful got route to destination", Toast.LENGTH_SHORT).show()
Log.d(TAG, "Successful got route to destination")}
})
以上操作会触发onResponse()
,表示路由请求成功,但是屏幕上没有呈现路由。只需显示我当前的位置(用下面的代码显示)即可:
private fun showUserLocation(style: Style){
if (PermissionsManager.areLocationPermissionsGranted(homeActivity)){
val locationComponentOptions = LocationComponentOptions.builder(homeActivity)
.bearingTintColor(Color.WHITE)
.accuracyAlpha(0.1f)
.build()
val locationComponentActivationOptions = LocationComponentActivationOptions
.builder(homeActivity, style)
.locationComponentOptions(locationComponentOptions)
.useDefaultLocationEngine(true)
.build()
locationComponent = mapbox.locationComponent
locationComponent?.activateLocationComponent(locationComponentActivationOptions)
locationComponent?.isLocationComponentEnabled = true
locationComponent?.cameraMode = CameraMode.TRACKING
locationComponent?.renderMode = RenderMode.COMPASS
createLocationEngine()
} else {
Toast.makeText(homeActivity, "Permissions not granted", Toast.LENGTH_LONG).show()
}
}
有人知道为什么不显示路线吗?