Hello there and welcome to my very first How To
This one goes out to our travelling salesmen, small scale logistic companies or our favorite Chinese take-away around the corner… or if, like me, you prefer the most time efficient route between various waypoints! With dashdash route optimization is just a few steps away
In Part 1, I will introduce you to a template for basic route optimization between various waypoints. We will look at a real life example, namely the three most visited places during my studies in NYC:
(A) NYU Library (B) Whole Foods (C) Favorite Doughnut Shop
There are only 4 steps to the optimal route
- Geocode your waypoints
- Grab an API key for Graphhopper
- Send the GET request for the route
- PARSE the request for routing instructions
If implemented correctly, you will be able to insert an address of your choosing and receive latitude and longitude values in return.
Grab an API key for Graphhopper
Visit Graphhopper for a free trial key.
Send GET request
Graphhopper offers various map-related APIs, but this time their standard Routing API will suffice.
The standard GET request looks as follows, whereas the bold parts mark the differing parameters and the italic parts are the parameters definitions:
You can add parameters to your GET request as your case requires. Refer to Graphhopper for documentation.
Let’s build our custom GET request. First, we need to define our parameters:
latitude point A A1 = 40.7295668 longitude point A A2 = -73.9965785 latitude point B B1= 40.7346359 longitude point B B2 = -73.9897659 latitude point C C1 = 40.7393418 longitude point C C2 = -73.9924034 locale D1 = en vehicle D2 = bike optimize D3 = TRUE API key D4 = dde361b4-0....
Now, let’s assemble our GET request!
Rookie tip on how to insert parameters to your request:
Add parameter by inserting &[parameter]= Define the parameter by (i) hardcoding the definition, e.g. &vehicle=car (ii) dynamically reference a cell, e.g. &vehicle="&D3&"
PARSE the request
Luckily, Graphhopper also provides documentation on how to PARSE the GET for the desired routing instructions.
We are interested in the route instructions, so we will PARSE as follows:
P0 =parse(G1,"paths.instructions.text") P1 =parse(G1,"paths.instructions.text") ... Pn =parse(G1,"paths.instructions[n].text")
PARSE until you make it … to “Arrive at destination”
And this is how you can define the optimal route between various waypoints with dashdash. Piece of cake, don’t you think?
Stay tuned for Part 2, where we will dive into more complex routing optimization cases that could involve various drivers, pick-up points and delivery locations!
P.S.: If you ever are in NY, try Dough Doughnuts on 14 W 19th St. They do actually serve the best around