Hands On
100DaysOfCode

# Solutions: Day 76-80 #100DaysOfCode

By Shruti Kuber | 23 June 2020

### Try HERE Maps

Create a free API key to build location-aware apps and services.

Welcome to week 16 of #100DaysOfCode with HERE. This week we are going to concentrate on the different costs involved in a journey and the ways to get cost optimized routes.
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-75, you can find them in the previous blogs posts or on our YouTube channel.
Let's begin!

### Day 76/100

When you are operating a fleet of commercial vehicles, it is necessary to consider the cost paid to drivers and the cost of the vehicle for the journey. With this information, you need to calculate the cost of different routes you can take to your destination and choose the one which is the most cost effective. This has to be done before planning your delivery schedules, and can take a lot of time. With the Fleet Telematics API, this has been simplified for developers. While getting a route for the vehicle, you can simply mention the driver_cost and the vehicle_cost in your target currency and get two things:

1. The total cost of the journey considering the driver cost and vehicle cost
2. Route options arranged to optimize cost and routing type

var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
alternatives:3,
driver_cost:10, // cost per hour
vehicle_cost:0.5, //cost per kilometer
currency:"EUR"
};


In the response, you will get an object called cost which will hold the information of the total cost for all the route options.


{
"response": {
"route": [
{
...,
"cost": {
"totalCost": "251.16",
"currency": "EUR",
"details": {
"driverCost": "48.98",
"vehicleCost": "202.18",
"optionalValue": "0.0"
}
}
},
{
...,
"cost": {
"totalCost": "271.7",
"currency": "EUR",
"details": {
"driverCost": "66.55",
"vehicleCost": "205.15",
"optionalValue": "0.0"
}
}
}
],
}
}


### Day 77/100

The cost calculated from day 76 is far from complete. There are more costs involved with the journey from point A to B. One of the obvious ones is the cost of fuel. To add that to the mix, the Fleet Telematics API lets you add fuel related details to the routing call. With day 77, we are adding the rate of consumption of fuel for this vehicle. This detail can help you get specific costs for all your vehicles. It is interesting to note that the parameter customConsumptionDetails lets you describe the speed profile of your vehicle along with the consumption profile. Say your truck consumes 7€ worth of fuel per 100 km at a speed of 60 km/hr and a different amount at reduced speed, different on a slope, urban areas etc. You can specify all this information in your consumption profile.
To make it easier, there are also some preset values for consumption profiles for trucks based on their weight. For this example, we will use the preset values for a 40 tonne truck. You will get this cost as energyCost under the cost object.


var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
alternatives:3,
driver_cost:10, // cost per hour
vehicle_cost:0.5, //cost per kilometer
currency:"EUR",
maxSpeed:"90",
customConsumptionDetails:"40t"
};


### Day 78/100

While we are talking about the fuel consumption profile, we should also consider the fuel type and the fuel cost. This is something that varies widely. Specifying this can get you the detailed cost for the journey. With the Fleet Telematics API, you can specify the fuel type and the fuel rate with parameters fuelType and costPerConsumptionUnit respectively. The truck I am considering runs on diesel and the current rate in Berlin, Germany, is about 1.225€/liter.


var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
alternatives:3,
driver_cost:10, // cost per hour
vehicle_cost:0.5, //cost per kilometer
currency:"EUR",
maxSpeed:"90",
customConsumptionDetails:"40t",
fuelType:"Diesel",
costPerConsumptionUnit:"1.225"
};


### Day 79/100

A major part of long-haul journeys is toll costs. Toll costs vary from country to country, region to region and are a reason for cumulative costs during the journey. With the Toll Cost API , you can make the routing engine consider toll costs. The Toll Cost API is part of the Fleet Telematics API and can give you toll summaries for the  route. For getting this cost, you need to mention certain details of your vehicle. Firstly, you need to specify the tollVehicleType as the toll cost is different for different vehicles. If your truck has trailers, mention the number of trailers. The warnings object in the response will tell you all toll-related parameters that you can mention to help the routing engine calculate the exact toll cost for your vehicle. After adding the details necessary to calculate the toll, you can specify the format in which you want to see the toll cost. This is done with the parameter rollups which is a necessary parameter for toll cost calculation. For this example, I have selected a total summary for the tolls involved. The toll cost of the journey is reflected in the cost object in the routing response as well as in a separate object called tollCost.


var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
alternatives:3,
driver_cost:10, // cost per hour
vehicle_cost:0.5, //cost per kilometer
currency:"EUR",
maxSpeed:"90",
customConsumptionDetails:"40t",
fuelType:"Diesel",
costPerConsumptionUnit:"1.225",
tollVehicleType:3,
trailersCount:1,
weightPerAxle:10,
rollups:"total"
};


### Day 80/100

From day 79, we saw that the parameter rollups can give us the total toll costs involved in the journey. This same parameter gives us other options to display the toll cost in the tollCost object of the response. The rollups parameter has options to display the total toll cost, costs per toll system, per country and per link. You can use the options in combinations separated with a comma to display different summaries for the toll costs. You can also use hybrid combinations like costs by country and toll systems by using a semicolon as the separator country;tollsys.


var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
alternatives:3,
driver_cost:10, // cost per hour
vehicle_cost:0.5, //cost per kilometer
currency:"EUR",
maxSpeed:"90",
customConsumptionDetails:"40t",
fuelType:"Diesel",
costPerConsumptionUnit:"1.225",
tollVehicleType:3,
trailersCount:1,
weightPerAxle:10,
rollups:"total,tollsys"
};


That was week 16 of #100DaysOfCode with HERE. This week was all about calculating journey related costs. Let us know if you would like to know more about journey costs with the Fleet Telematics 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 how to create a Flutter Map Application in under 5 minutes with this blog post by our 🥑 Richard Süselbeck.
Happy coding!