Hands On

HERE Fleet Telematics: Calculate Toll Costs Along a Route

By Shruti Kuber | 29 January 2020

Try HERE Maps

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

Get Started

Fleet management applications not only give you an overview of the health of your fleet, but also ways to optimize your operations in terms of time and costs. One of the ways to optimize the fleet is to be aware of all the costs involved in the route before scheduling it. Toll costs may not seem like a lot individually, but they add up over time. This blog is a continuation of the Fleet Telematics series and explores the TOLL COST API and its features.

If you have been following our previous blogs, you might have noticed that we have revised our authentication types with improved security. So, if you are still using the APP_ID and APP_CODE methods of authentication, I would encourage you to get your APIKEY from your projects page. For those who do not have an account yet, sign up for a freemium account at developer.here.com. Now, with new (great?) authentication types, come new (great?) domain names. Any Spidey fans? No? OK :(. Take a look at what the new domains look like for HERE Location Services. For this blog, we are going to take a look at the FLeet Toll Cost API and so the domain fleet.ls.hereapi.com

Lets begin with setting up a simple routing call for a truck with our new domain. You can also refer to the Truck routing blog for this.


    let api ='fleet.ls.hereapi.com/2/calculateroute.json',
            apiKey = 'YOUR REST KEY',
            waypoint0 = '38.72639,-9.14949',
            waypoint1 = '47.54881,7.58782',
            mode = 'fastest;truck;',
            representation ='display',

            url = `https://${api}?apiKey=${apiKey}&waypoint0=${waypoint0}&detail=1&waypoint1=${waypoint1}&mode=${mode}&representation=${representation}`;
            fetch(url)
                .then(response => response.json())
                .then(response => {
                    console.log(response)
                }, error => {
                    console.error(error);
                });

What we have here is a simple routing call with waypoint0 as the starting point and waypoint1 as the end point and truck as the mode of transport. So far, the response does not look very different from that with the basic routing domain route.ls.hereapi.com/routing/7.2/calculateroute.json


    response: {…}
​​      language: "en-us"
​​      route: (1) […]
        ​0: {…}​​​
            boatFerry: false
            ​​​​leg: (1) […]
                ​​​​​0: Object { length: 2046937, travelTime: 79774, trafficTime: 79774, … }
                ​​​​​length: 1
            ​​​​mode: {…}
            ​​​​​   trafficMode: "disabled"
            ​​​​​   transportModes: Array [ "truck" ]
            ​​​​​   type: "fastest"
            ​​​​railFerry: false
            ​​​​summary: {…}
                ​​​​​baseTime: 79774
                ​​​​​distance: 2046937
                ​​​​​flags: Array []
                ​​​​​trafficTime: 79774
                ​​​​​travelTime: 79774
            ​​​​waypoint: (2) […]
            ​​​​​   0: Object { linkId: "+996218283", spot: 0.49411, sideOfStreet: "neither", … }
                ...

Now we add the magic parameter!
Harry: What is it?
Dumbledore: It's Love!
I promise this was the last one. The parameter is tollVehicleType. Add that to the parameters and you will have: fetch(https://${api}?apiKey=${apiKey}&waypoint0=${waypoint0}&detail=1&waypoint1=${waypoint1}&mode=${mode}&representation=${representation}&tollVehicleType=truck). The response will have a new parameter under response.route[]


    cost: {…}
        ​​​​​currency: "EUR"
        ​​​​​details: {…}
            ​​​​​​driverCost: "0.0"
            ​​​​​​optionalValue: 0
            ​​​​​​tollCost: "397.65"
            ​​​​​​vehicleCost: "0.0"

As you can see, you will get the total toll cost for the route in the response. Adding driver_cost and vehicle_cost to the url parameters will also add the total driver and vehicle cost for the route.
Is that enough though? Knowing the total toll cost for the trip helps, but what helps even more is a detailed toll booth to toll booth analysis of it. If you take a look at the documentation for the Fleet Toll Cost API, you will see that with this API, you can get details like:-

  • total summary
  • cost per link road
  • cost per country
  • cost per toll system

This can be achieved by adding a simple yet powerful parameter called rollups The rollups parameter decides the level of detail you want with the toll costs. Thus the options are:

  • none -toll cost per link
  • total -total toll costs
  • tollsys -toll cost per toll system summary
  • country -toll cost per country summary
  • country;tollsys -toll cost per country and toll system summary

Your final call will look something like this:

 


    let api ='fleet.ls.hereapi.com/2/calculateroute.json',
            apiKey = 'YOUR REST APIKEY',
            waypoint0 = '38.72639,-9.14949',
            waypoint1 = '47.54881,7.58782',
            mode = 'fastest;truck;',
            representation ='display',
            tollVehicleType = 'truck',
            driver_cost = '40',
            vehicle_cost = '0.5',
            rollups = 'none,tollsys,country,total',

            url = `https://${api}?apiKey=${apiKey}&waypoint0=${waypoint0}&detail=1&waypoint1=${waypoint1}&mode=${mode}&representation=${representation}&driver_cost=${driver_cost}&vehicle_cost=${vehicle_cost}&tollVehicleType=${tollVehicleType}&rollups=${rollups}`;
            fetch(url)
                .then(response => response.json())
                .then(response => {
                    console.log(response)
                }, error => {
                    console.error(error);
                });

The response will include:


    response: {…}
​​      language: "en-us"
    ​   route: (1) […]
    ​​​     0: {…}
    ​​​​        boatFerry: false
            ​​​​cost: {…}
                ​​​​​currency: "EUR"
                ​​​​​details: Object { driverCost: "889.35", vehicleCost: "1013.16", tollCost: "303.85", … }
                ​​​​​totalCost: "2206.37"
            ​​​​​leg: Array [ {…} ]​​​​
            mode: Object { type: "fastest", transportModes: (1) […], trafficMode: "disabled" }   ​​​​
            railFerry: false    ​​​​
            summary: Object { travelTime: 80041, distance: 2026324, baseTime: 80041, … }    ​​​​
            tollCost: {…}    ​​​​​
                costsByCountry: (4) […]    ​​​​​​
                    0: Object { country: "PRT", amountInTargetCurrency: 41.2 }    ​​​​​​
                    1: Object { country: "ESP", amountInTargetCurrency: 28.49 }   ​​​​​​
                    2: Object { country: "FRA", amountInTargetCurrency: 233.1 }    ​​​​​​
                    3: Object { country: "CHE", amountInTargetCurrency: 1.06 }    ​​​​​​
                    length: 4    ​​​​​​   ​​​​​
                costsByTollSystem: (8) […]    ​​​​​​
                    0: Object { tollSystemId: "5004", name: "BRISA", languageCode: "ENG", … }    ​​​​​​
                    1: Object { tollSystemId: "7249", name: "SCUTVIAS", languageCode: "ENG", … }    ​​​​​​
                    2: Object { tollSystemId: "5023", name: "ASCENDI", languageCode: "ENG", … }    ​​​​​​
                    3: Object { tollSystemId: "5055", name: "BIDEGI", languageCode: "ENG", … }    ​​​​​​
                    4: Object { tollSystemId: "7607", name: "ASF", languageCode: "ENG", … }    ​​​​​​
                    5: Object { tollSystemId: "7155", name: "ATLANDES", languageCode: "ENG", … }    ​​​​​​
                    6: Object { tollSystemId: "5016", name: "APRR", languageCode: "ENG", … }    ​​​​​​
                    7: Object { adminId: "20138137", amountInTargetCurrency: 1.06 }    ​​​​​​
                    length: 8
                onError: false
    ​​​​​            routeTollItems: (171) […]
                    [0…99]
                    [100…170]    ​​​​​​
                    length: 171
                .....

From here you can extract the detailed cost, choose routes with lower toll costs or visualize the different toll booths along the routes and provide the driver a notification in advance.
If you are interested in a detailed usage of the Toll Cost API, check out the tutorial by my colleague Sudheer. Until then, happy mapping!