首页 > 解决方案 > 在 Mapbox Android 中添加坐标

问题描述

我正在使用mapbox android,我正在尝试在起点和目的地之间添加多个航点。但是在添加一个航点后,当它添加另一个航点后,会出现异常“s 坐标太多;最大坐标数为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");
            }
        });

    }}

标签: androidnavigationmapbox-android

解决方案


请求路由的默认配置文件是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);
        }
    }

推荐阅读