Parse paths to use Mapbox utilities

Hi,

I’m trying to parse the response of a routing request into a MapBox route. The following code produces as successful response and allows me to make use of the round trip feature of the routing API:

List<String> point = new ArrayList<>();
String coord = locations[0].getLatitude() + "," + locations[0].getLongitude();
point.add(coord);
RoutingApi api = new RoutingApi();
RouteResponse res = api.routeGet(
                point, false, "[KEY]", "en",  true,
                "foot", false, true, null, true, null, false, "round_trip", null, null,
                null, null, (int) 3, (long) 1, null, null, null, null);

However I cannot figure out how to turn this response into a route using the MapBox functions? Can anyone please point me in the right direction?

Thanks
Alex

When you use Java, I recommend to use our hand crafted client instead. It is not so ugly to create a simple routing request:

We also provide a /navigate endpoint with which it should be possible to use a mapbox client directly. See our navigation example that uses the Mapbox Navigation SDK: https://github.com/graphhopper/graphhopper-navigation-example

1 Like

Thanks for the tip @karussell.

I have tried to implement the hand crafted client, but i keep getting 405 errors (“Method not allowed”).

    // Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
    GraphHopperWeb gh = new GraphHopperWeb();
    // insert your key here
    gh.setKey(KEY);
    // change timeout, default is 5 seconds
    gh.setDownloader(new OkHttpClient.Builder().
            connectTimeout(5, TimeUnit.SECONDS).
            readTimeout(5, TimeUnit.SECONDS).build());

    GHRequest req = new GHRequest()
            .addPoint(new GHPoint(51.9073273, -0.2260282))
            .addPoint(new GHPoint(51.9373273, -0.2560282))
            .setLocale(Locale.ENGLISH)
            .setVehicle("car");
    req.getHints().put("instructions", true);
    req.getHints().put("calc_points", true);
    GHResponse res = gh.route(req);

Can anyone tell me why this might be the case, i have tried all sorts of combinations, cutting it back to the simplest possible and i have made no progress.

You need to use the latest stable version, not from master. Which version are you using?

This is my Gradle dependency:

implementation 'com.graphhopper:directions-api-client-hc:1.0-pre4'

Can you try the latest stable 0.13.0 instead?

1 Like

Have tried the following dependency and now works perfectly:

implementation 'com.graphhopper:directions-api-client-hc:0.13.0-pre9'

Thanks for the info :smile:

And 0.13.0 does not work?

“pre” means it is sometimes not fully production ready and also not supported later on with security bug fixes. We only support the latest 0.13.0.

1 Like

Yes 0.13.0 works, I will use that instead of 0.13.0-pre9, thanks for the advice!

I am still having trouble integrating the response from the Routing API into the Mapbox navigation endpoint.

Correct me if i’m wrong, but the navigation endpoint in the example SDK uses https://graphhopper.com/api/1/navigate/ to generate an encoded polyline which is used by the Mapbox API to generate the route, whereas the HC routing api uses https://graphhopper.com/api/1/route to generate a routing response which does not include the correct information to pass to the mapbox API.

How do i turn the response of the Routing API into something that can be used by the Navigation endpoint, without creating a second Routing request?

If you read /route use client-hc and convert to the format you need.
If you read /navigate you should be able to use any mapbox library, e.g. the Java library like we do in our Android demo.

How do i turn the response of the Routing API into something that can be used by the Navigation endpoint, without creating a second Routing request?

I do not understand this. The navigation endpoint is an endpoint to serve a client just like the /route endpoint ‘only’ in a different JSON format, it does not consume something from a source.

1 Like

Thank you for your help, my question was probably a bit vague. After some sleuthing, I think i have found what i need, so I will post it here for anyone else.

Interesting - thanks!

Looks like I have not properly understood your use case. For the Routing API you do not need an extra node.js service though, we did this in Java here: https://github.com/graphhopper/graphhopper-navigation

1 Like

Perfect, exactly what I needed! Thanks!