【问题标题】:Adding coordinate in Mapbox Android在 Mapbox Android 中添加坐标
【发布时间】:2021-05-22 05:58:32
【问题描述】:

我正在使用 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");
            }
        });

    }}

【问题讨论】:

    标签: android navigation mapbox-android


    【解决方案1】:

    在 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

    【讨论】:

    • 不幸的是,我不是在寻找这个,我需要用多个航点绘制路线,我用那里的文档尝试过,但不能在上面添加多个航点。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多