Search code examples
javamapbox-android

mapbox android add waypoints


How can I put waypoints in mapbox? I've tried this method but it still doesn't work.

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) {

                    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);

                    Log.d("LOGcurrent", String.valueOf(currentRoute));

                    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());
                }
            });
}

Solution

  • Try using MapboxDirections to create a client as follows.

    MapboxDirections.Builder builder = MapboxDirections.builder()
                        .origin(origin)
                        .destination(destination)
                        .accessToken(context.getString(R.string.mapbox_access_token));
    

    Now, you can call this to add a waypoint:

    builder.addWaypoint(your_waypoint);
    

    Then create a client with the above directions builder and use the client you've created to enqueue call to receive a route response.

    MapboxDirections client = builder.build();
    client.enqueueCall(new Callback<DirectionsResponse>(){
       ...
    });