我正在使用mapbox android,我正在尝试在起点和目的地之间添加多个航路点。但是在添加一个航路点后,当它添加另一个航路点时,会出现异常“ s 坐标太多;最大坐标数为3.”
我只想在两点之间添加多个路径点并在mapbox android中的这些线上绘制路线。
[过去的bin链接]:https://paste.ubuntu.com/p/PKMQzFyzVb/
我的路线绘制功能 -->
{
private void getRouteWithWaypoint(Point origin, Point destination, List<Point> wayPoints) {
assert Mapbox.getAccessToken() != null;
NavigationRoute.Builder builder = NavigationRoute.builder(getActivity())
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.destination(destination);
if (wayPoints != null) {
for (Point point : wayPoints) {
builder.addWaypoint(point);
}
}
builder.build().getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(@NonNull Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
Log.e(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.");
return;
} else if (response.body().routes().size() < 1) {
Log.e(TAG, "No routes found");
return;
}
currentRoute = response.body().routes().get(0);
if (navigationMapRoute != null) {
navigationMapRoute.removeRoute();
} else {
navigationMapRoute = new NavigationMapRoute(null, mapView, map, R.style.NavigationMapRoute);
}
navigationMapRoute.addRoute(currentRoute);
}
@SuppressLint("TimberArgCount")
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
Timber.e(t, "Error: %s");
}
});
}}
在 Mapbox 地图上绘制根,从 Mapbox 文档复制以下代码。
private void getRoute(Point origin, Point destination) {
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.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.");
return;
} else if (response.body().routes().size() < 1) {
Log.e(TAG, "No routes found");
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());
}
});
}
欲了解更多详情,请点击链接 https://www.mapbox.com/help/android-navigation-sdk/#calculate-and-draw-route
请求路由的默认配置文件是
DirectionsCriteria.ProfileCriteria.PROFILE_DRIVING_TRAFFIC
此配置文件仅允许出发地和目的地之间有 1 个航路点。如果您想使用 1 个以上的航点,只需使用
PROFILE_DRIVING
即可(我认为最多可以使用 25 个航点)。
像这样:
NavigationRoute.Builder builder = NavigationRoute.builder(getActivity())
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.destination(destination)
.profile(DirectionsCriteria.ProfileCriteria.PROFILE_DRIVING);
if (wayPoints != null) {
for (Point point : wayPoints) {
builder.addWaypoint(point);
}
}
首先,你可以得到这样一条路: (MAPBOX DIRECTIONS V10 KOTLIN)
private fun makeRequestToGetRoute(
originPoint: Point,
destination: Point,
isDirectionToDriver: Boolean
) {
val routeOptions = RouteOptions.builder()
.applyDefaultNavigationOptions()
.coordinatesList(listOf(originPoint, destination))
.waypointsPerRoute(true)
.alternatives(false)
.build()
mapboxNavigation.requestRoutes(routeOptions, object : NavigationRouterCallback {
override fun onCanceled(routeOptions: RouteOptions, routerOrigin: RouterOrigin) {
}
override fun onFailure(reasons: List<RouterFailure>, routeOptions: RouteOptions) {
}
override fun onRoutesReady(
routes: List<NavigationRoute>, routerOrigin: RouterOrigin
) {
mapboxNavigation.setNavigationRoutes(routes) { _ ->
}
drawPolyLine(routes.first().directionsRoute.completeGeometryToPoints())
}
})
}
这里,我调用了drawPolyline函数,它使用点在地图上绘制线条。 功能代码如下:
private fun drawPolyLine(pointList: List<Point>) {
Log.d(TAG, "drawPolyLine: called draw")
val polylineAnnotationManager: PolylineAnnotationManager =
mapView.annotations.createPolylineAnnotationManager()
val polylineAnnotationOptions = PolylineAnnotationOptions()
.withPoints(pointList)
.withLineColor("#ee4e8b")
.withLineWidth(4.0)
polylineAnnotationManager.create(polylineAnnotationOptions)
}
使用这个功能,你可以画它。 withLineColor("#ee4e8b") 设置颜色,用 .withLineWidth(4.0) 设置线条。
它适用于我的项目中的mapbox v10,所以尝试一下。祝你好运