Get array of ordered points (with lat, lon informations)

I’m using Route Optimization API in my project, and I would like to know how to access in someway the points ordered after optimization. What I need is to get the lat,lon couple of the points that I have added to the request, ordered according to the optimization, 'cause I want to show them on the map with a popup marked with the order number they “received”.

I’ve read this part of documentation looking for a useful output parameter and, apart from paths[0].points_order that gives me the new order of points, I couldn’t manage to find anything else, or how to use parameters. I’ve only found paths[0].points and paths[0].snapped_waypoints that could be the ones to give me the ordered infos, but I’m not sure which one I should use and how. I’ve tried setting points_encoded to false in the request, since in the docs they say:

If points_encoded=true or no points_encoded specified an encoded string will be returned, otherwise an array with order [lon,lat,elevation] is returned.

But I don’t know how to access this information to achieve what I want, and which between points or snapped_waypoints I should use :sweat:

EDIT:

I’v tried this query:

https://graphhopper.com/api/1/route?point=44.1388386%2C12.243707&point=44.1374648%2C12.2480796&point=44.1359114%2C12.2454082&point=44.1371501&2C12.24147&vehicle=foot&locale=de&points_encoded=false&key=[my_key]

In the result I have a lot of lat,lon couple inside the “points” array, and just 3 out of 4 points specified in snapped_waypoints, that aren’t even the points I’ve specified. What do these results represent? Apparently I can’t use them to achieve my objective.
Also, if using the ghRouting request in javascript code, points_encoded is still considered to be true even if I set it to false

I think you missed adding optimize=true.

Have a look at the following request:
https://graphhopper.com/api/1/route?point=48.257599%2C8.959351&point=48.447422%2C9.060974&point=48.413593%2C9.471245&point=48.412454%2C9.477596&point=48.459035%2C9.42524&point=48.411884%2C9.034882&point=48.312428%2C9.624023&locale=en-au&vehicle=car&weighting=fastest&points_encoded=falseoptimize=true

Snapped_waypoinst contains your original order.

snapped_waypoints: {
coordinates: [
[
8.959514,
48.257391
],
[
9.034655,
48.411829
],
[
9.060651,
48.447392
],
[
9.425163,
48.459003
],
[
9.471301,
48.413616
],
[
9.477556,
48.412735
],
[
9.624217,
48.311714
]
],
type: "LineString"
}

Then there is the points_order array:

points_order: [
0,
5,
1,
4,
2,
3,
6
]

The first entry references the first snapped_point, which is put in order 0. The second point is put at the order 5, the third point is put into position 1, and so on.

1 Like

I forgot to write it in the request, sorry, I did write “optimize:true”.
What confused me is that the points in snapped_waypoints aren’t the same you put in the request. They are modified a little, why is that?

That’s why they are called snapped_waypoints and not waypoint ;). They get snapped to the closest road and are used for route planning then.

3 Likes