我已经尝试在我的项目中实现更快的路由检测和替代路由。但是我从文档中尝试的代码似乎无法正常工作。在这里,我将提供代码以更好地理解。
MapboxNavigationOptions mapOptions = MapboxNavigationOptions.builder()
.build();
mapOptions.enableFasterRouteDetection();
mapboxNavigation = new MapboxNavigation(MapBoxActivity.this, Mapbox.getAccessToken(), mapOptions);
mapboxNavigation.addFasterRouteListener(new FasterRouteListener() {
@Override
public void fasterRouteFound(DirectionsRoute directionsRoute) {
// Update MapboxNavigation here
// navigation.startNavigation(directionsRoute);
mapboxNavigation.startNavigation(directionsRoute);
}
});
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.alternatives(true)
.destination(destination)
.build()
.getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
// You can get the generic HTTP info about the response
Log.d(TAG, "Response code: " + response.code());
if (response.body() == null) {
Log.e(TAG, "No routes found, make sure you set the right user and access token.");
Toast.makeText(MapBoxActivity.this, "No routes found, make sure you set the right user and access token.", Toast.LENGTH_SHORT).show();
return;
} else if (response.body().routes().size() < 1) {
Log.e(TAG, "No routes found");
Toast.makeText(MapBoxActivity.this, "No routes found.", Toast.LENGTH_SHORT).show();
return;
}
currentRoute = response.body().routes().get(0);
// Draw the route on the map
if (navigationMapRoute != null) {
navigationMapRoute.removeRoute();
} else {
navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap, R.style.NavigationMapRoute);
}
navigationMapRoute.addRoute(currentRoute);
}
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
Log.e(TAG, "Error: " + throwable.getMessage());
}
});
请有人指出我犯了什么错误。预先感谢您的帮助。