Solutions: Day 51-55 #100DaysOfCode
It's week 11 of #100DaysOfCode with HERE and we have accomplished so much already. We have gone through the Interactive Maps API, the Geocoding and Search API and the Routing v8 API. With this week, we will start exploring 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-50, you can read them in the previous blogs posts or on our YouTube channel.
Let's begin!
Day 51/100
With Day 51, we have also started the avoid series where we try to avoid certain parts of the route while getting a route from point A to B. Also, we will be using the Routing v7 API. In this task, we will use the pedestrian route from day 50, but avoid parks on the way. Why avoid parks? Well because I am super allergic to pollen and walking through a park is sadly not a walk in the park 😓. So lets get the route from day 50, but with Routing v7
. The API structure is pretty much the same. The origin, destination
are replaced with waypoint0, waypoint1
respectively, while transportMode, routingMode
are combined as mode
. Remember to call the Routing v7 API.
// 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;pedestrian,
representation: "display"
};
Now, to avoid parks, the mode
parameter has an attribute called RouteFeature
which lists features that you can avoid on a route. These features come with weights depending on how badly you want to avoid them. For this task, we want to avoid parks, so we give the weight -1
. This way, the routing API will give penalties to routes with parks. Which means you might still get a route going through the park, but it will be later in the alternatives. You can change up the weights for softer or harder restrictions.
// 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;pedestrian;park:-1",
representation: "display"
};
Day 52/100
Walking can get you only up to a certain distance. For longer distances, lets go biking. Its a good exercise and you're also controlling your carbon footprint. To do this, the routing v7 API lets you add bicycle
as your routing mode.
// 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;bicycle",
representation: "display"
};
Day 53/100
Again, the routing mode bicycle
lets you avoid things too. This time, we are avoiding dirt roads. My bicycle is a weak little city bike and won't be able to survive dirt roads. So lets avoid them. You will find dirt roads as a RouteFeatureType
under RouteFeature
. This time I want to strictly avoid the dirt roads, so I will assign it a weight of -3
.
// 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;bicycle;dirtRoad:-3",
representation: "display"
};
Day 54/100
So we're done walking and biking. Let's do the next best thing. Let's use public transport. Again, we're using the same schematic as day 52 and just switching up the routing mode
// 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;publicTransport",
representation: "display"
};
Day 55/100
As much as I love to travel, I also have motion sickness. I know what you're thinking. This girl has too many issues, right? Welcome to my life! Well, among all modes of tansport, buses make me the most sick. So... let's avoid them too. This time, there is a special parameter to avoid things for public transport. This parameter is called avoidTransportTypes
. This parameter comes with a list of PublicTransportTypeType
that you can avoid. We are going to avoid the public buses, coz c'mon, I can't give up on Flixbus.
// Create the parameters for the routing request:
var routingParameters = {
waypoint0:"52.4569927,13.380545",
waypoint1:"52.4805740,13.4303771",
mode:"fastest;publicTransport",
avoidTransportTypes:"busPublic",
representation: "display"
};
That was week 11 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 - Create an android map application in under 5 minutes! by our 🥑 Richard Süselbeck.
Happy coding!
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