Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for directions responses with "geojson" geometries #1219

miptildar opened this issue Nov 28, 2020 · 7 comments

Add support for directions responses with "geojson" geometries #1219

miptildar opened this issue Nov 28, 2020 · 7 comments


Copy link

I am working on a project where our users will be sharing the same route and use Mapbox in their client apps for navigation.
Since it is important for us to use the same route, it was decided to generate route on a backend side and clients were suppose to download route in json format, parse it and give route to Mapbox Navigation SDK.
I tried to solve this task in 2 ways.

1. Using REST API

I tried to use your REST API for generating a route and it works well, but when I try to parse JSON using DirectionsRoute.fromJson(String json) method I am getting an exception

Expected a string but was BEGIN_OBJECT at line 1 column 969 path $.legs[0].steps[0].geometry

although JSON itself has a valid format.

2. Using Java SDK

On a backed side I was generating a route, but using Mapbox SDK:

String accessToken =

        Point originPoint = Point.fromLngLat(**, **);
        Point destinationPoint = Point.fromLngLat(**, **);

        MapboxDirections client = MapboxDirections.builder()

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

                DirectionsRoute directionsRoute = response.body().routes().get(0);
                Gson gson = new Gson();
                String json = gson.toJson(directionsRoute, DirectionsRoute.class);


            public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
                System.out.println("Error: " + throwable.getMessage());

But this time converting DirectionsRoute to json returns {}, although route array is not empty.

I only need to generate route on backend side as json and use that route on client side.
Any of these solutions are suitable for us if they will work properly.
Could you help me resolve any of them?

Copy link

miptildar commented Nov 30, 2020

Okay, let me simplify testing sequence.

public class MapBoxRequest {

  private static final String PATTERN =

  final String ENCODING = "UTF-8";

  public static void main(String[] args) throws IOException, URISyntaxException {
    HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();

    String accessToken =

    // set right location values
    String string = String.format(PATTERN, longitude1, latitude1, longitude2, latitude2, accessToken);

      URI uri = new URI(string);

    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(uri.toString()));
    String rawResponse = request.execute().parseAsString();


Just try to run this code.

I am getting

Exception in thread "main" java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 546 path $.routes[0].legs[0].steps[0].geometry
	at com.mapbox.api.directions.v5.models.DirectionsResponse.fromJson(
	at dating.walking.service.walking.setup.MapBoxRequest.main(
Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 546 path $.routes[0].legs[0].steps[0].geometry

And maven dependency:



Copy link

There seems to be an issue indeed. I can reproduce the issue on my side, using exactly the same json as provided by the Directions API or trying a minimized version with less information - shouldn't this just work out of the box?

Copy link

TheNewCivilian commented Mar 19, 2021

@miptildar Actually after having a closer look into this issue it seams like this might not be necessarily a bug.
Investigating the de-serialization process, it turns out several subobjects are de-serialized sequentially.

This means i.e. the geometry property you passed needs to be still a string when passed to the related de-serializer.
I suppose this will fix your issue from above.

Copy link

TheNewCivilian commented Mar 20, 2021

Working example routes can be found here:
These routes hold the geometry as "polyline6" as mapbox calls it.
So your code above might finally work if you set:
private static final String PATTERN = ",%s;%s,%s?alternatives=true&geometries=polyline6&steps=true&access_token=%s";

Copy link

@TheNewCivilian makes the right point - this library only supports polyline and polyline6 encoded geometries, even though the Directions API also supports the LineString (geojson) encoding. This is why the deserializer expects a string in the geometry field, not a list of points.


* @param geometries null if you'd like the default geometry, else one of the options found in
* {@link GeometriesCriteria}.
* @return this builder for chaining options together
* @since 2.0.0
public abstract Builder geometries(@GeometriesCriteria String geometries);
* Retention policy for the various direction geometries.
* @since 3.0.0
@StringDef( {
public @interface GeometriesCriteria {

We can keep this ticket around as a feature request.

@LukasPaczos LukasPaczos changed the title Create route on a backend side and share a route with Android/iOS clients as a JSON Add support for directions responses with "geojson" geometries Jun 3, 2021
Copy link

Has this been fixed?

Copy link

This is what I do on backend side

import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.MapboxDirections;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;

private final Gson gson =
            new GsonBuilder()

private String getRoute(Point point1, Point point2) throws IOException {
        MapboxDirections client =

        Response<DirectionsResponse> responseFor12 = client.executeCall();
        if (responseFor12.body() == null) {
            LOGGER.error("No routes found, make sure you set the right user and access token.");
        } else if (responseFor12.body().routes().size() < 1) {
            LOGGER.error("No routes found");
        } else {
            DirectionsRoute directionsRoute = responseFor12.body().routes().get(0);
            return gson.toJson(directionsRoute);

        return null;

This is how I parse json on Android side

Gson gson =
                    new GsonBuilder()
DirectionsRoute parsedFromJsonRoute = gson.fromJson(jsonAsString, DirectionsRoute.class);

And it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

6 participants