Use Routing API with optimize:true

I’m using Routing API and I would like to optimize the route. I’ve installed the api js client and I’m performing the request this way:

require('graphhopper-js-api-client')
var GraphHopperRouting = require('graphhopper-js-api-client/src/GraphHopperRouting');
var GHInput = require('graphhopper-js-api-client/src/GHInput');
var ghRouting = new GraphHopperRouting({key: defaultKey, host: host, vehicle:profile,elevation:false})
ghRouting.addPoint(new GHInput(43.9124756, 12.915549400000032));
ghRouting.addPoint(new GHInput(43.767771, 13.138918399999966));
ghRouting.addPoint(new GHInput(43.7262567, 12.636563300000034));

In the routing docs it says that to optimize the route I have to specify optimize:true to sort the points, and that I can use that only if I have route optimization API package. Since the api js client has all the APIs, I simply specified optimize:true, but the points aren’t sorted (checked the correct sorting in the live example tool). Is there something else I have to do? How is the code supposed to be? Here is explained how to use routing API, so I don’t know what else is required to use route optimization.
Also I’ve tried to do this:

 var GHOptimizitation = require('graphhopper-js-api-client/src/GraphHopperOptimization');
 var ghRouting = new GHOptimization({key: defaultKey, host: host, vehicle:profile,elevation:false,optimize:true})

but the following error is shown while performing the request:

Cannot read property 'vehicle_types' of undefined

even if I set vehicle_types instead of vehicle in the request

Try at least 4 points as the first and last point is fixed. If you need an ‘open’ optimization you currently would have to use the Route Optimization API (with the GraphHopperOptimization) with calc_points:true in the routing configuration.

Cannot read property ‘vehicle_types’ of undefined

Please see the code and documentation about this API to learn how you can make it working

Hi, sorry for the late response.
I’ve tries using more points and the result is a routing with points in order of declaration.

Yep, that’s what I’m trying to do, but the GHOptimization doesn’t seem to work; also, in the docs for Route Optimization API , as far as I see, is not specified how to write the request like for the routing api (there are just the endpoints).
Also, where can I modify the routing configuration as you said?
Sorry for all the questions but I’m stuck

Please avoid cross posting - it creates unnecessary duplicative work for us: https://github.com/graphhopper/directions-api-js-client/issues/26

I’ve tries using more points and the result is a routing with points in order of declaration.

I’ll have a look into this.

in the docs for Route Optimization API

In the Javascript code you create a specific JSON where the properties are documented. This JSON is then posted to the endpoint via the Javascript client and a JSON is returned when it is ready. Have a look into the demo project using the route optimization client.

Also, where can I modify the routing configuration as you said?

I’m referring to this routing configuration

I’ve tries using more points and the result is a routing with points in order of declaration.

Open this html in your browser. It will log an object that contains a points_order element which is not 1,2,3,4,5 but it is 0,3,1,2,4 and the points and instructions entries also changed to match this order.

I’m sorry for crossposting, didn’t know they are linked.
Thanks for the example, now it seems to work.
Just a little question, what does the 0 stand for in the points_order element returned, and why there is no 5? Just out of curiosity, since the points are displayed correctly on the map.

Sorry, I meant 0,1,2,3,4 not 1,2,3,4,5. The indices go from 0 to size-1

1 Like