在Mapbox Android中添加坐标



我使用的是mapbox android,我试图在起点和终点之间添加多个航点。但在添加一个航点后,当添加另一个时,会出现异常"坐标太多;最大坐标数为3。">

我只想在两点之间添加多个航路点,并在mapbox android中绘制出这条线上的路线。

[pastbin链接]: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-并绘制路线

请求路由的默认配置文件是DirectionsCriteria.ProfileCriteria.PROFILE_DRIVING_TRAFFIC

此配置文件只允许在起点和目的地之间有一个航路点。如果你想使用一个以上的航路点,只需使用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);
}
}

最新更新