在Mapbox Android中添加坐标

时间:2018-11-02 05:05:58

标签: android navigation mapbox-android

我正在使用mapbox android,正在尝试在起点和目的地之间添加多个航路点。但是在添加一个航路点并添加另一个航路点后,这会产生异常 “。 ;最大座标数为3。“

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

[垃圾箱链接]: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");
            }
        });

    }}

1 个答案:

答案 0 :(得分:0)

在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