Routing Truck Rest Stops with TomTom Maps

Use CodePen to create a routing app with a reachable range so that truckers can time their rest stops.

TomTom Developers
TomTom Developers
7 min readSep 5, 2021

--

Most map applications track routes from a start address to a destination address, which only gives you the route and time. TomTom’s Maps SDK for Web provides comprehensive APIs to allow you to build map applications with many exciting features. For example, we can create a truck routing application for truck drivers to choose rest stops after a specified driving time.

TomTom’s developer portal provides live functional examples. We can build our application based on existing examples that already have the essential functions we need.

This tutorial reviews how to build the truck routing application based on the Supporting Points example in CodePen. The Supporting Points example displays the best route between two points and allows adding stops by clicking on the map. To meet the needs of our use case, we can simply use the Reachable Range API to display a vehicle-reachable range within the specified time for the start point and each supporting point that the user picks.

Follow the tutorial in this article to build an application in CodePen and see the results immediately inside CodePen’s live editor. The only thing you need is a browser.

GETTING STARTED

First, let’s do a little bit of housekeeping. We will need a TomTom developer account. If you don’t have one, register an account with TomTom.

Log into the TomTom Developer Portal and get an API key for the Map Display API, Search API, and Routing API.

Once you get your API key, do the following:

This opens the CodePen online live editor with all the HTML, CSS, and JS code. We’ll use this as a starting point and build as we go. But, if you’d like to peek at the final application, you can find it here.

CALCULATING A ROUTE

We can start interacting with the Supporting Points example by entering the start and the destination addresses. The map displays a route. We can then click on the map to add supporting points.

The Supporting Points example calculates the best route between the start point and the destination point, passing through supporting points using the tt.services.calculateRoute function. The markers are added using the tt.Markerfunction.

ADDING UI ITEMS

The Supporting Points example already has the input fields for the start and destination addresses. What we need is to add a time input field and a button to submit its value.

In the HTML box, add the following code before the </header> tag:

Also change the result header text from “Supporting Points” to “Rest Stops” for our use case. To do so, find <div class=’result-header’>Supporting Points:</div>, and change it to <div class=’result-header’>Rest Stops:</div>.

Add the following code at the end of the CSS box. The code formats the Driving Time Interval input field:

FINDING A REGION

Next, enter a desired driving time interval in seconds, then click Find Region to see the reachable region overlaid on the map.

Use Calculate Reachable Range to find the reachable region within the time interval of the origin. To do so, provide the origin and the timeBudgetInSec parameters to the tt.services.calculateReachableRange function:

  • The origin parameter is a pair of longitude and latitude lines for the “origin point.”
  • The timeBudgetInSec parameter is the maximum driving time interval in seconds.

The request returns a JSON object that contains a polygon boundary in counterclockwise orientation and the precise polygon center (the result of map-matching the “origin point”). We can add a map layer that fills the polygon with the desired color and opacity with the returned results. We define the handleServiceResponse and the drawPolygon functions to handle this.

Add the following JavaScript code at the end of the JS box:

Users can then pick a rest stop within the polygon or anywhere they want. The map displays the rest stop address when clicking on it. This recalculates the route from the origin to the chosen rest stop.

When users select their first stop location in the Rest Stops section and click Find Region again, the map no longer shows the reachable region. To do this, we must define a cleanMap function for it.

Add the following JavaScript code at the end of the JS box:

Recalculate the reachable region using the new location. Use the new location as the “origin point” to calculate the new reachable area and overlay it on the map.

Add the following JavaScript code at the end of the JS box:

You’ll need to insert your own API key after key: as noted in the code above.

REMOVING A REGION

Users can pick the next rest stop and so on until they finally reach their destination.

Remove the last reachable region and keep all the rest stops on the map. To do so, add another button to remove the reachable area. Users can also use this button to remove the reachable area at any time.

In the HTML box, add the following code right after the “Find Region” input tag:

Add the following JavaScript code at the end of the JS box:

CONCLUSION

And that’s it! We’ve just created a truck routing application in CodePen. Now, you can play with it. If you haven’t looked at the final application in CodePen yet, you can find it here.

In this tutorial, you have learned how to create a truck routing application that allows truck drivers to pick rest stops within a specified driving time. You can see how easy it’s to make an application based on an existing example in TomTom’s developer portal. To see other examples of a reachable region, check out Reachable Region and Batch Reachable Region.

For more information about the APIs used in this use case, including additional options and capabilities, you can review the documentation about Search API, Routing API, and Map Display API at any time.

This article was originally published at https://developer.tomtom.com/blog.

--

--

TomTom Developers
TomTom Developers

We love maps. We love data. We love developers. We’re here to help developers build the next generation of location-based applications.