mapbox-java
mapbox-java copied to clipboard
Add support for directions responses with "geojson" geometries
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 =
"accees_token";
Point originPoint = Point.fromLngLat(**, **);
Point destinationPoint = Point.fromLngLat(**, **);
MapboxDirections client = MapboxDirections.builder()
.origin(originPoint)
.destination(destinationPoint)
.overview(DirectionsCriteria.OVERVIEW_FULL)
.profile(DirectionsCriteria.PROFILE_WALKING)
.accessToken(accessToken)
.build();
client.enqueueCall(new Callback<>() {
@Override
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);
}
@Override
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?
Okay, let me simplify testing sequence.
public class MapBoxRequest {
private static final String PATTERN =
"https://api.mapbox.com/directions/v5/mapbox/walking/%s,%s;%s,%s?alternatives=true&geometries=geojson&steps=true&access_token=%s";
final String ENCODING = "UTF-8";
public static void main(String[] args) throws IOException, URISyntaxException {
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
String accessToken =
"access_toekn";
// 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();
DirectionsResponse.fromJson(rawResponse);
}
}
Just try to run this code.
I am getting
Exception in thread "main" com.google.gson.JsonSyntaxException: 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.google.gson.Gson.fromJson(Gson.java:939)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.mapbox.api.directions.v5.models.DirectionsResponse.fromJson(DirectionsResponse.java:133)
at dating.walking.service.walking.setup.MapBoxRequest.main(MapBoxRequest.java:34)
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
at com.google.gson.stream.JsonReader.nextString(JsonReader.java:825)
And maven dependency:
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.38.0</version>
</dependency>
<dependency>
<groupId>com.mapbox.mapboxsdk</groupId>
<artifactId>mapbox-sdk-services</artifactId>
<version>5.6.0</version>
</dependency>
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?
@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.
Working example routes can be found here: https://github.com/mapbox/mapbox-navigation-android/tree/main/examples/src/main/res/raw
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 = "https://api.mapbox.com/directions/v5/mapbox/walking/%s,%s;%s,%s?alternatives=true&geometries=polyline6&steps=true&access_token=%s";
@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.
See https://github.com/mapbox/mapbox-java/blob/d7dc8cd86ef5a215d96c378fdfa3789f9caa77ce/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java#L506-L511 and https://github.com/mapbox/mapbox-java/blob/d7dc8cd86ef5a215d96c378fdfa3789f9caa77ce/services-directions-models/src/main/java/com/mapbox/api/directions/v5/DirectionsCriteria.java#L253-L264.
We can keep this ticket around as a feature request.
Has this been fixed?
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()
.registerTypeAdapterFactory(DirectionsAdapterFactory.create())
.create();
private String getRoute(Point point1, Point point2) throws IOException {
MapboxDirections client =
MapboxDirections.builder()
.origin(point1)
.destination(point2)
.overview(DirectionsCriteria.OVERVIEW_FULL)
.profile(DirectionsCriteria.PROFILE_WALKING)
.steps(true)
.accessToken(mapboxAccessToken)
.voiceInstructions(false)
.language(Locale.ENGLISH)
.bannerInstructions(false)
.build();
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);
directionsRoute.geometry();
return gson.toJson(directionsRoute);
}
return null;
}
This is how I parse json on Android side
Gson gson =
new GsonBuilder()
.registerTypeAdapterFactory(DirectionsAdapterFactory.create())
.create();
DirectionsRoute parsedFromJsonRoute = gson.fromJson(jsonAsString, DirectionsRoute.class);
And it works