Skip to main content


The Mapbox Optimization API returns a duration-optimized route between the input coordinates. This is also known as solving the Traveling Salesperson Problem. A typical use case for this API is planning the route for deliveries in a city. An optimized route can be retrieved for car driving, bicycling, walking, or hiking.

For more information about this API, including its pricing structure, see the Mapbox Optimization API documentation.

The wrapper for the Mapbox Optimization API is included in the mapbox-sdk-services module. Before using this wrapper, make sure that you have included the correct permissions inside your AndroidManifest.xml file if you plan to use this API inside of an Android application. The MapboxOptimization class is used to return a duration-optimized route between the input coordinates.

Optimization request

Before making the Optimization API request, you must build the MapboxOptimization object passing in three required parameters:

  1. A valid Mapbox access token.
  2. A <List> of Point objects. These are the waypoints in the optimized route. The minimum amount of Point objects in the list is 2, and the maximum is 12.
  3. A directions profile. The API needs to know whether you want a route which is optimized for driving, cycling, or walking.

Here's an example MapboxOptimization object:

private List<Point> coordinates;


MapboxOptimization optimizedClient = MapboxOptimization.builder()

You can read about optional parameters in the Optimization API documentation.

Optimization response

Once you have built your MapboxOptimization object with all the parameters that you'd like to use in the request, you'll need to send the request using enqueueCall() asynchronously. Once the request receives a response, it will tell the callback where you can handle the response appropriately.

optimizedClient.enqueueCall(new Callback<OptimizationResponse>() {
public void onResponse(Call<OptimizationResponse> call, Response<OptimizationResponse> response) {

if (!response.isSuccessful()) {
Log.d(TAG, "optimization call not successful");
} else {
if (response.body().trips().isEmpty()) {
Log.d(TAG, "optimization call successful but no routes");

DirectionsRoute optimizedRoute = response.body().trips().get(0);


public void onFailure(Call<OptimizationResponse> call, Throwable throwable) {
Log.d(TAG, "Error: " + throwable.getMessage());

In case your user leaves the activity or application before the callback's notified, you should use cancelCall() within your onDestroy() lifecycle method:

protected void onDestroy() {

if (optimizedClient != null) {
Was this page helpful?