Solutions: Day 71-75 #100DaysOfCode
Welcome to week 15 #100DaysOfCode with HERE. In this week, we will learn to customize the response that you get from the routing request with Routing v7.
If you have no idea what I'm talking about, take a look at this blog post which will tell you everything about #100DaysOfCode. If you have missed the solutions for days 0-70, you can read them in the previous blogs posts or on our YouTube channel.
Let's begin!
Day 71/100
Looking at all the objects in the response of the routing call and selecting the ones that make sense can be overwhelming. With day 71, we will look at how to get the summary of a route per country when your route crosses countries. We have selected a route from Berlin, Germany to Warsaw, Poland for the example. The rest of the parameters are basic. What changes is what you want in the route response. The routeAttributes
parameter lets you customize what you want in your route response. For this example, we will take a look at the property summaryByCountry
which should give is the route summary per country that it crosses.
var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
routeAttributes:"summaryByCountry"
};
In the response, you will see details of the route for every country with distance traveled and time taken in each of the countries.
summaryByCountry: (2) […]
0: {…}
_type: "RouteSummaryByCountryType"
baseTime: 5769
country: "DEU"
distance: 104702
flags: Array(4) [ "tollroad", "tunnel", "motorway", … ]
text: "...""
tollRoadDistance: 103179
trafficTime: 6207
travelTime: 6207
1: {…}
_type: "RouteSummaryByCountryType"
baseTime: 14446
country: "POL"
distance: 299131
flags: Array(4) [ "tollroad", "tunnel", "motorway", … ]
text: "..."
tollRoadDistance: 170174
trafficTime: 16393
travelTime: 16393
length: 2
Day 72/100
With day 72, we are going to add another parameter to the routeAttributes
, namely: zones
. With this parameter, you will get the list of all the routing zones that your vehicle will pass through. This is important when looking at restrictions on vehicles in specific routing zones. To add more objects to the parameter, you can simply separate them with a comma. You can also use shortened versions of the option names. So summaryByCountry
can be written as sc
, zones
as zo
, and so on. You can find out all the short versions here.
var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
routeAttributes:"summaryByCountry,zones"
};
Day 73/100
If you analyze the route object, you will see that the first object within it is the leg
. This object gives details of every leg of the route and is often useful in analyzing different parts of the route. With the legAttributes
, you can customize the information you get in the leg
object of your route response. To get the maneuvers per leg of the route, we can simply add the legAttributes
as maneuvers
.
var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
routeAttributes:"summaryByCountry,zones",
legAttributes:"maneuvers"
};
Day 74/100
With maneuvers, you also want to display the direction of the maneuver. You can use this with a text-to-speech converter as an instruction to the driver. Check out all the possible maneuver attributes that you can define. For day 74, we can simply include direction
as our maneuverAttributes
.
var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
routeAttributes:"summaryByCountry,zones",
legAttributes:"maneuvers",
maneuverAttributes:"direction"
};
Day 75/100
And finally for day 75, we will take a look at cleaning up our response. As much as you can add to a response, sometimes you do not need as many objects in your response. To completely delete an object from your response, you can set the value for it's parameter as none
. To selectively remove attributes, you can add a negative sign -
before the attribute name and you will get a cleaner response.
var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
routeAttributes:"-summary",
legAttributes:'none',
maneuverAttributes:"none"
};
That was week 15 of #100DaysOfCode With HERE. I hope this week was helpful in understanding the responses of the Routing API v7. Let us know if you would like to know more about some of the responses of the Routing API. Keep following us on Twitter for more tasks and complete all 100 days. If you want to watch the video version of these solutions, take a look at our playlist for #100DaysOfCode on YouTube. If you want the code snippets of the APIs covered with #100DaysOfCode, head over to the 100daysofcode GitHub repository.
Meanwhile, learn more about BoundingBox through this blog post by our 🥑 Michael Palermo. While you're at it, learn how to work with geojson in the browser with our very own Jedi Master Raymond Camden.
Happy coding!
Have your say
Sign up for our newsletter
Why sign up:
- Latest offers and discounts
- Tailored content delivered weekly
- Exclusive events
- One click to unsubscribe