Skip to main content
APIs 4 min read

Solutions: Day 56-60 #100DaysOfCode

Solutions: Day 56-60 #100DaysOfCode

We are in week 12 of #100DaysOfCode with HERE! With this week, we will deep dive into the Routing v7 API.
If you have no idea what I'm talking about, take a look at this blog post which will tell you everything about #100DaysOfCode. If you have missed the solutions for days 0-55, you can read them in the previous blogs posts or on our YouTube channel.
Let's begin!

Day 56/100

Avoiding traffic jams is as easy as it gets! Just ask politely 😝. Well, almost. Just indicate the TrafficMode to enabled. You will see the returned routes have arranged themselves in such a way that the route avoiding traffic is the first route returned.

Copied
        
// Get an instance of the routing service version 7:
var router = platform.getRoutingService();

// Create the parameters for the routing request:
var routingParameters = {
  waypoint0:"52.4569927,13.380545",
  waypoint1:"52.4805740,13.4303771",
  mode:"fastest;car;traffic:enabled",
  alternatives:3,
  representation: "display"
};

  

Day 57/100

For whatever reasons, sometimes, you want to avoid certain areas while driving. With the routing API, it is possible in two simple steps. The first is to identify the area and draw a box around it. Drawing a rectangle is similar to drawing any map object that we learnt in week 3 of 100daysofcode. To draw this rectangle, we are going to use the method H.geo.Rect.fromPoints to draw the rectangle by stating the top-left and bottom-right points of the rectangle.

Copied
        

let rectangle = new H.map.Rect( H.geo.Rect.fromPoints({"lat":52.47196092299903,"lng":13.376929060418977},{"lat":52.46853475142611,"lng":13.386257162576658}),{
  style:{
    strokeColor:'red',
    fillColor:'rgba(255,0,0,0.5)'
  }
});
  
map.addObject(rectangle);

  

Day 58/100

To avoid the area that we drew on day 57, the second step is to use the avoidAreas parameter. Within this parameter, we will pass the bounding box we drew on day 57. The format of passing this bounding box is top-left and bottom-right geo-coordinates of the box.

Copied
        
// Get an instance of the routing service version 7:
var router = platform.getRoutingService();

// Create the parameters for the routing request:
var routingParameters = {
  waypoint0:"52.4569927,13.380545",
  waypoint1:"52.4805740,13.4303771",
  mode:"fastest;car;traffic:enabled",
  avoidAreas:rectangle.getBoundingBox().getTopLeft().lat+','+
  rectangle.getBoundingBox().getTopLeft().lng+';'+
  rectangle.getBoundingBox().getBottomRight().lat+','+
  rectangle.getBoundingBox().getBottomRight().lng,
  representation: "display"
};

  

Day 59/100

A route is divided into several legs and these route legs are further divided into links. We will use these link ids to identify the patched of roads we wish to avoid. To do this, we need to use the parameter legAttributes and pass the RouteLegAttributeType as links

Copied
        
  // Get an instance of the routing service version 7:
  var router = platform.getRoutingService();
  
  // Create the parameters for the routing request:
  var routingParameters = {
    waypoint0:"52.4569927,13.380545",
    waypoint1:"52.4805740,13.4303771",
    mode:"fastest;car;traffic:enabled",
    alternatives:3,
    representation: "display",
    legAttributes:'links'
  };

  

Day 60/100

We will now use the linkID obtained from day 59 to avoid that portion of the road. To do this, you need to pass the linkId in the parameter avoidLinks.

Copied
        
  // Create the parameters for the routing request:
  var routingParameters = {
    waypoint0:"52.4569927,13.380545",
    waypoint1:"52.4805740,13.4303771",
    mode:"fastest;car;traffic:enabled",
    alternatives:3,
    representation: "display",
    avoidLinks:'-1258798441'
  };

  

That was week 12 of #100DaysOfCode With HERE. Keep following us on Twitter for more tasks and complete all 100 days. If you want to watch the video version of these solutions, take a look at our playlist for #100DaysOfCode on YouTube. If you want the code snippets of the APIs covered with #100DaysOfCode, head over to the 100daysofcode GitHub repository.
While you're at it, why don't you take a look at the blog post - Controlling the map size by our 🥑 Michael Palermo.
Happy coding!

Shruti Kuber

Shruti Kuber

Have your say

Sign up for our newsletter

Why sign up:

  • Latest offers and discounts
  • Tailored content delivered weekly
  • Exclusive events
  • One click to unsubscribe