Fetching total distance and travel time

Hi there,

May I ask how do I fetch my estimated travel time and distance by using your routing API with android studio? I found the page here https://docs.graphhopper.com/#tag/Routing-API.
but I do not know how should I WRITE THE CODE can I fetch the details using your okhttpclient. I think this may be a simple json response problem, but please help me if possible cuz I stuck here for a long time.

Really thanks for your assistance.



Hi I would just google it?

For example use this: https://android.jlelse.eu/rest-api-on-android-made-simple-or-how-i-learned-to-stop-worrying-and-love-the-rxjava-b3c2c949cad4

In the docs you linked, you can find how the request-url should look like.

Thanks, but may I ask thru the graphhopper routing api I posted it write that I shall use this code. Is it possible to get distance and time thru adding somthing to this code?

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()

    Response response = client.newCall(request).execute();

Hi Grace,
I think you should use GHRequest.

So something like this:
GHRequest req = new GHRequest(startLat, startLon, endLat, endLon)
GHResponse rsp = hopper.route(req);

Then get best time like:
and distance:
You can go view the docs on GHRequest for more

1 Like

Is GHRequest and GHResponse not if you have the server running yourself? I think Grace is asking to call the api instead of running the server locally.

The distance and time should be in the response body variable. There should be an object with the values you are looking for. Also you have to add your api_key instead of key. Otherwise it won’t work.

Hi @diane.wilma, thanks for your help! I would like to try it. But may I ask is it necessary for me to clode the whole graphhopper here in order to use the class of GHrequest? Cuz cloning will rewrite every other things I have currently(ex. gradle, manifest…etc) Is there any other easier way to use GHrequest class?

Hi @easylite376, There sure is the object. as the document below stated in the pic, but how do I acquire them? I thought I can use response.body().distance but it isn’t working though QWQ

I can’t help you without trying it myself, but just use the debug mode to look at the body element of the response and with that you know how to get the distance and time.

Distance could also be the first element of the array, because you get an array of things.

@Grace If you are using Java or Android the “client-hc” will help as @diane.wilma pointed out. Use e.g. version 0.13.0-pre12: https://search.maven.org/search?q=g:com.graphhopper%20AND%20a:directions-api-client-hc&core=gav

Hi @karussell, @diane.wilma
thanks for your information and assitance, but do you mean I download the file thru your link and use her ghrequest code?
I already add this line to my gradle.build

implementation ‘com.graphhopper:directions-api-client-hc:0.13.0-pre12’

But my ghrequest still can’t work. ghrequest can be resolved, what can not be resolve is the parameters after, may I ask can you please tell me how to solve this problem?
tks a lot for your kind support



Ah, sorry for my stupid mistakes. I shall set my parameters into my own parameters. But what I’m confused now is that how can I get my current position.latlng from graphhopper? I’ve already used locationEngine and locationLayerPlugin, but it’s said to be deprecated and can not work. So I change into LocationComponent, but it’s version crash with graphhopper when I add

implementation ‘com.mapbox.mapboxsdk:mapbox-android-sdk:6.1.0’
implementation (‘com.mapbox.mapboxsdk:mapbox-android-sdk:6.1.0’){
exclude group: ‘group_name’, module: ‘module_name’ }

into my gradle these dependencies in order to resolve locationComponent. Does anyone know how to solve this?><