Hands On

Avoiding Areas Using the HERE Routing API

By Richard Süselbeck | 05 March 2020

Try HERE Maps

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

Get Started

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. Things like specific roads, sketchy areas and bus rides. Don't avoid this new series of blog posts, where we will cover all of them.

Today we will learn how to avoid areas!

First, let’s look at a standard call to our Routing API (v7).


There’s not 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 this.


Note that you can check out this example in our Web SDK to see how to display a route on map. Conveniently, the JavaScript API uses the same routing parameters as the REST API. Have a look at this code snippet, which calls the Routing API in JS.

function calculateRoute(platform) {
  var router = platform.getRoutingService(),
    routeRequestParams = {
      mode: 'fastest;car',
      waypoint0: '52.5160,13.3779', 
      waypoint1: '52.5185,13.4283'      


To make sure that this route avoids a specific area, we can use the avoidareas parameter of the Routing API. We need to pass it a bounding box using two locations (top-left and bottom-right of the box).


Let’s draw this area on the map and see if the new route takes a path around it.


Note that if you want to draw the area on the map using our Web SDK, you can use a map object such as H.map.Rect as seen in this code snippet.

function drawAreas(map) {    
  var firstArea = new H.map.Rect(new H.geo.Rect(52.51856,13.39751,52.51602,13.40431));  

Looks like avoiding things is pretty easy! Let’s avoid some more things and add another area to avoid. The Routing API supports avoiding up to 20 areas at once. To do so, we can simply add more bounding boxes and separate them with an exclamation mark.


One last time, let’s draw this on a map and see what happens.



Success! Stay tuned for more posts on how to avoid things like highways and sharp turns. (No news yet on how to avoid personal responsibility, but I’ve filed a ticket with a feature request.)