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).


https://route.ls.hereapi.com/routing/7.2/calculateroute.json
?apiKey=your_apiKey
&mode=fastest;car
&waypoint0=52.53086,13.38474
&waypoint1=52.54417,13.39156

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.

devblog-routing-avoiding-areas-1


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'      
    };

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

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).


https://route.ls.hereapi.com/routing/7.2/calculateroute.json
?apiKey=your_apiKey
&mode=fastest;car
&waypoint0=52.530861,13.38474
&waypoint1=52.54417,13.39156
&avoidAreas=52.54226,13.39165;52.54135,13.39457

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

devblog-routing-avoiding-areas-2



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));  
  map.addObject(firstArea);  
}

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.


https://route.ls.hereapi.com/routing/7.2/calculateroute.json
?apiKey=your_apiKey
&mode=fastest;car
&waypoint0=52.530861,13.38474
&waypoint1=52.54417,13.39156
&avoidAreas=52.54226,13.39165;52.54135,13.39457!52.523,13.41447;52.52041,13.42069

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

devblog-routing-avoiding-areas-3

 

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.)