KarlSC
KarlSC

Reputation: 47

Mapbox Directions API returns unusable response with status code 422

I am using MapBox Directions with android and I'm trying to just get the directions to later draw on the map.

However, the response I get from the call is never usable as the response.body() returns null.

When I call response.code() i get the status code 422, which according to the Mapbox API means "The given request was not valid. The message key of the response will hold an explanation of the invalid input."

The response.message() returns the string "Unknown".

response.errorBody() returns the string "okhttp3.ResponseBody$1@38fb6f04"

I have used the same code as this stack overflow answer which should work, but it doesn't. Any help would be appreciated.

Heres my method:

    private void getRoute(Position origin, Position destination) throws ServicesException {

    MapboxDirections client = new MapboxDirections.Builder()
            .setOrigin(origin)
            .setDestination(destination)
            .setProfile(DirectionsCriteria.PROFILE_CYCLING)
            .setAccessToken("Same access token as the map uses")
            .build();

    client.enqueueCall(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;
            }

            // Print some info about the route
            currentRoute = response.body().getRoutes().get(0);
            Log.d(TAG, "Distance: " + currentRoute.getDistance());
            Toast.makeText(MainActivity.this, "Route is " +  currentRoute.getDistance() + " meters long.", Toast.LENGTH_SHORT).show();

            // Draw the route on the map
            drawRoute(currentRoute);
        }

        @Override
        public void onFailure(Call<DirectionsResponse> call, Throwable t) {
            Log.e(TAG, "Error: " + t.getMessage());
            Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

And my imports:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.mapbox.services.directions.v5.DirectionsCriteria;
import com.mapbox.services.directions.v5.MapboxDirections;
import com.mapbox.services.directions.v5.models.DirectionsResponse;
import com.mapbox.services.directions.v5.models.DirectionsRoute;

And my gradle:

    compile ('com.mapbox.mapboxsdk:mapbox-android-services:1.0.0@aar'){
    transitive=true
}

I have tried with different positions in different countries with the as well as a new access token with ass permissions turned on with the same results. What am I doing wrong?

Thanks a lot.

Upvotes: 2

Views: 2239

Answers (2)

Angel Guevara
Angel Guevara

Reputation: 426

Check if you are setting the latitude and longitude in the right way when you build the Position object.

The contructor is important.

Position pos = Position.fromCoordinates(/*LON FIRST*/lon, /*LAT SECOND*/ lat);

Example:

Position origin = Position.fromCoordinates(location.getLongitude(),location.getLatitude());

Hope this help to you or future consultants.

Upvotes: 6

cammace
cammace

Reputation: 3168

Looks like you are using 1.0.0 still, we've updated to 1.1.0 which bring stability to directions V5. If updating doesn't resolve the issue, you might be passing your origin/destination coordinates in the wrong order, it should be longitude, latitude; not latitude, longitude. Lastly, if this doesn't fix the issue, i'd love to look at the actual url request, could you print it into log and edit your question.

Upvotes: 1

Related Questions