Hands On

Avoiding Traffic Jams using the HERE Routing API

By Richard Süselbeck | 01 April 2020

Avoiding things is fun! For example, I love to avoid traffic jams, parties and personal responsibility. Some of these are harder to avoid than others, but fortunately our Routing API is here to help! It provides us with a whole range of options to calculate routes that avoid things, from sketchy areas to bus rides. Don't avoid this ongoing series of blog posts, in which we will cover all of them. Today we will learn how to avoid traffic jams!

First, let’s look at a standard call to our Routing API (v7) for a short drive in London.


https://route.ls.hereapi.com/routing/7.2/calculateroute.json
?apiKey=your_api_key
&waypoint0=51.44221,-0.08096
&waypoint1=51.44224,-0.03834
&mode=fastest;car

There’s not too much to explain here. We have two waypoints, a routing mode (we’re asking for the fastest route by car) and an apikey for authentication (sign up for our Freemium plan to get 250.000 routing calls for free every month).

If we display this route on a map, it looks like the following:

devblog-routing-avoiding-trafficjams-1

Note that you can check out this example in our Web SDK documentation on how to display a route on a map. As you can see in this code snippet, the JavaScript API uses the same routing parameters we are discussing here.


function calculateRouteFromAtoB (platform) {
  var router = platform.getRoutingService(),
    routeRequestParams = {
      mode: 'fastest;car',
      waypoint0: '51.44221,-0.08096', 
      waypoint1: '51.44224,-0.03834',      
    };

  router.calculateRoute(
    routeRequestParams,
    onSuccess,
    onError
  );
}

Now if we turn on traffic flow on the map, we see that this route goes right through a massive traffic jam on the South Circular Road. (Note that this was true, when I was running these examples. Current traffic conditions will of course be different for you. Feel free to experiment using your favorite traffic-prone area.)

devblog-routing-avoiding-trafficjams-2

You can turn on traffic flow for any of our web maps, simply by creating default layers and adding a default UI. This will add UI elements to the bottom right of the map, where you can toggle various layers, including traffic flow. Once again, check the example linked above on how to do this.

Because we can see the traffic flow on the map, we obviously have live traffic information available. In order to avoid the traffic jam, we need to make sure the Routing API takes this information into account. To so, we change the routing mode as follows.


https://route.ls.hereapi.com/routing/7.2/calculateroute.json
?apiKey=your_api_key
&waypoint0=51.44221,-0.08096
&waypoint1=51.44224,-0.03834
&mode=fastest;car;traffic:enabled

Once again, let’s draw this on a map and see what happens.

devblog-routing-avoiding-trafficjams-3

We can see that the route has changed. While we can’t avoid part of the traffic jam to the east (likely due to the limited number of railroad crossings, which make diversions tricky), taking the longer route around the park is actually faster!

Another feature of the Routing API that can help you avoid traffic jams, is the ability to calculate several alternative routes. This allows you to look at several routing options and pick the one that is best suited for you. To do this, we need to add the alternatives parameter. In the following example, we are asking for three alternatives.


https://route.ls.hereapi.com/routing/7.2/calculateroute.json
?apiKey=your_api_key
&waypoint0=51.44221,-0.08096
&waypoint1=51.44224,-0.03834
&mode=fastest;car;traffic:enabled
&alternatives=3

And that’s it for avoiding traffics jams! Next time, we’ll learn how avoid my least favorite form of public transport, the bus!