Skip to content

Commit 67ac3bb

Browse files
committed
Truck routing with road restrictions style
Signed-off-by: German Zargaryan <[email protected]>
1 parent 01a053f commit 67ac3bb

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ All of the following examples use **version 3.1** of the API
5757
* [Take a Snapshot of the Map](https://heremaps.github.io/maps-api-for-javascript-examples/capture-map-area/demo.html) - Capture an area on the map
5858
* [Zoom into Bounds](https://heremaps.github.io/maps-api-for-javascript-examples/custom-zooming-into-bounds/demo.html) - Zoom into bounds limiting maximum level
5959
* [Zooming to a Set of Markers](https://heremaps.github.io/maps-api-for-javascript-examples/zoom-to-set-of-markers/demo.html) - Alter the viewport to ensure a group of objects are visible
60+
* [Truck routing road restrictions](https://heremaps.github.io/maps-api-for-javascript-examples/truck-routing-road-restrictions/) Show a various truck routes with the truck related road restrictions highlighted on the map
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#map {
3+
width: 95%;
4+
height: 450px;
5+
background: grey;
6+
}
7+
8+
#panel {
9+
width: 100%;
10+
height: auto !important;
11+
font-weight: bold;
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Map with the truck routes from A to B and road restrictions
3+
description: Show a various truck routes with the truck related road restrictions highlighted on the map
4+
resources:
5+
- https://heremaps.github.io/maps-api-for-javascript-examples/test-credentials.js
6+
7+
normalize_css: no
8+
dtd: html 5
9+
wrap: d
10+
panel_html: 0
11+
panel_js: 0
12+
panel_css: 0
13+
...
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
5+
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
6+
<title>Map with the truck routes and road restrictions</title>
7+
<link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
8+
<link rel="stylesheet" type="text/css" href="demo.css" />
9+
<link rel="stylesheet" type="text/css" href="styles.css" />
10+
<link rel="stylesheet" type="text/css" href="../template.css" />
11+
<script type="text/javascript" src='../test-credentials.js'></script>
12+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
13+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
14+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
15+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
16+
</head>
17+
<body id="markers-on-the-map">
18+
19+
<div class="page-header">
20+
<h1>Map with the truck routes and road restrictions</h1>
21+
<p>Show a truck routes with the truck related road restrictions highlighted on the map</p>
22+
</div>
23+
<p>This example calculates and displays 3 different truck routes with the same start and end positions,
24+
but various truck attributes.
25+
<br>
26+
The start position is in <b>Manhattan</b> and the end position on the opposite bank in <b>Newport</b>.
27+
<p>
28+
<ul>
29+
<li>The blue route is a simple truck route (without specifying any truck attributes).</li>
30+
<li>The green route shows that a truck must use <b>Lincoln Tunnel</b> instead of <b>Holland Tunnel</b>
31+
when towing a trailer (uses the <code>trailersCount</code> parameter of the Routing API).</li>
32+
<li>The violet route shows that a truck with a trailer of flammable goods should take a much longer detour
33+
(uses the <code>shippedHazardousGoods=flammable</code> parameter of the Routing API).</li>
34+
</ul>
35+
</p>
36+
<p>More information about above mentioned routing request parameters can be found <a href="https://developer.here.com/documentation/routing/dev_guide/topics/resource-calculate-route.html" target="_blank">here</a>.</p>
37+
<p>
38+
The truck restrictions layer is set as a base map.
39+
</p>
40+
<div id="map"></div>
41+
<div id="panel"></div>
42+
<h3>Code</h3>
43+
<p>Access to the routing service is obtained from the <code>H.service.Platform</code> by calling
44+
<code>getRoutingService()</code>. The <code>calculateRoute()</code> method is used to calculate the fastest
45+
truck (<code>mode:fastest;truck</code>) route by passing in the relevant parameters as defined in
46+
<a href="http://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-route.html" target="_blank">Routing API</a>.
47+
The styling and display of the response is under the control of the developer.</p>
48+
<script type="text/javascript" src='demo.js'></script>
49+
</body>
50+
</html>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* A full list of available request parameters can be found in the Routing API documentation.
3+
* see: http://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-route.html
4+
*/
5+
var routeRequestParams = {
6+
mode: 'fastest;truck',
7+
waypoint0: 'geo!40.7249546323,-74.0110042', // Manhattan
8+
waypoint1: 'geo!40.7324386599,-74.0341396', // Newport
9+
representation: 'display',
10+
routeattributes : 'waypoints,summary,shape,legs',
11+
'metricSystem': 'imperial'
12+
};
13+
14+
function calculateRoutes(platform) {
15+
var router = platform.getRoutingService();
16+
17+
// The blue route showing a simple truck route
18+
calculateRoute(router, routeRequestParams, {
19+
strokeColor: 'rgba(0, 128, 255, 0.7)',
20+
lineWidth: 10
21+
});
22+
23+
// The green route showing a truck route with a trailer
24+
calculateRoute(router, Object.assign(routeRequestParams, {
25+
trailersCount: 1
26+
}), {
27+
strokeColor: 'rgba(25, 150, 10, 0.7)',
28+
lineWidth: 7
29+
});
30+
31+
// The violet route showing a truck route with a trailer
32+
calculateRoute(router, Object.assign(routeRequestParams, {
33+
trailersCount: 1,
34+
shippedHazardousGoods: 'flammable'
35+
}), {
36+
strokeColor: 'rgba(255, 0, 255, 0.7)',
37+
lineWidth: 5
38+
});
39+
}
40+
41+
/**
42+
* Calculates and displays a route.
43+
* @param {Object} params The Routing API request parameters
44+
* @param {H.service.RoutingService} router The service stub for requesting the Routing API
45+
* @param {mapsjs.map.SpatialStyle.Options} style The style of the route to display on the map
46+
*/
47+
function calculateRoute (router, params, style) {
48+
router.calculateRoute(params, function(result) {
49+
addRouteShapeToMap(style, result.response.route[0]);
50+
}, console.error);
51+
}
52+
53+
/**
54+
* Boilerplate map initialization code starts below:
55+
*/
56+
57+
// set up containers for the map + panel
58+
var mapContainer = document.getElementById('map'),
59+
routeInstructionsContainer = document.getElementById('panel');
60+
61+
// Step 1: initialize communication with the platform
62+
// In your own code, replace variable window.apikey with your own apikey
63+
var platform = new H.service.Platform({
64+
apikey: window.apikey
65+
});
66+
67+
var defaultLayers = platform.createDefaultLayers();
68+
69+
// Step 2: initialize a map - this map is centered over Berlin
70+
var map = new H.Map(mapContainer,
71+
// Set truck restriction layer as a base map
72+
defaultLayers.vector.normal.truck,{
73+
center: {lat: 40.745390, lng: -74.022917},
74+
zoom: 13.2,
75+
pixelRatio: window.devicePixelRatio || 1
76+
});
77+
// add a resize listener to make sure that the map occupies the whole container
78+
window.addEventListener('resize', () => map.getViewPort().resize());
79+
80+
// Step 3: make the map interactive
81+
// MapEvents enables the event system
82+
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
83+
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
84+
85+
86+
/**
87+
* Creates a H.map.Polyline from the shape of the route and adds it to the map.
88+
* @param {Object} route A route as received from the H.service.RoutingService
89+
*/
90+
function addRouteShapeToMap(style, route){
91+
var lineString = new H.geo.LineString(),
92+
routeShape = route.shape,
93+
polyline,
94+
div = document.createElement('div');
95+
96+
div.innerHTML = route.summary.text;
97+
div.style.color = style.strokeColor;
98+
document.getElementById('panel').appendChild(div);
99+
100+
routeShape.forEach(function(point) {
101+
var parts = point.split(',');
102+
lineString.pushLatLngAlt(parts[0], parts[1]);
103+
});
104+
105+
polyline = new H.map.Polyline(lineString, {style});
106+
107+
// Add the polyline to the map
108+
map.addObject(polyline);
109+
}
110+
111+
// Now use the map as required...
112+
calculateRoutes (platform);

truck-routing-road-restrictions/styles.css

Whitespace-only changes.

0 commit comments

Comments
 (0)