Skip to content

Commit 7d10366

Browse files
dbacarelvedha98
andauthored
Switch truck-routing-road-restrictions example to use HARP (#173)
Co-authored-by: vedha gnanam <[email protected]>
1 parent deb9b96 commit 7d10366

File tree

2 files changed

+88
-55
lines changed

2 files changed

+88
-55
lines changed

truck-routing-road-restrictions/demo.html

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<link rel="stylesheet" type="text/css" href="../template.css" />
1111
<script type="text/javascript" src='../test-credentials.js'></script>
1212
<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-harp.js"></script>
1314
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
1415
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
1516
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
@@ -20,17 +21,15 @@
2021
<h1>Map with the truck routes and road restrictions</h1>
2122
<p>Show a truck routes with the truck related road restrictions highlighted on the map</p>
2223
</div>
23-
<p>This example calculates and displays 3 different truck routes with the same start and end positions,
24-
but various truck attributes.
24+
<p>This example calculates and displays 2 different truck routes with the same start and end positions,
25+
but different truck attributes.
2526
<br>
2627
The start position is in <b>Manhattan</b> and the end position on the opposite bank in <b>Newport</b>.
2728
<p>
2829
<ul>
2930
<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 it has four axles (uses the <code>truck[axleCount]</code> parameter of the Routing API).</li>
32-
<li>The violet route shows that a truck with the four acles transporting flammable goods should take a much longer detour
33-
(uses the <code>truck[shippedHazardousGoods]</code> parameter of the Routing API).</li>
31+
<li>The fuchsia route shows that a truck with four axles and one trailer transporting flammable goods should take a much longer detour
32+
(uses <code>vehicle[axleCount], vehicle[trailerCount], vehicle[hazardousGoods]</code> parameters of the Routing API).</li>
3433
</ul>
3534
</p>
3635
<p>More information about above mentioned routing request parameters can be found <a href="https://www.here.com/docs/bundle/routing-api-v8-api-reference/page/index.html#tag/Routing/operation/calculateRoutes" target="_blank">here</a>.</p>

truck-routing-road-restrictions/demo.js

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,59 @@
22
* A full list of available request parameters can be found in the Routing API documentation.
33
* see: https://www.here.com/docs/bundle/routing-api-v8-api-reference/page/index.html#tag/Routing/operation/calculateRoutes
44
*/
5-
var routeRequestParams = {
6-
routingMode: 'fast',
7-
transportMode: 'truck',
8-
origin: '40.7249546323,-74.0110042', // Manhattan
9-
destination: '40.7324386599,-74.0341396', // Newport
10-
return: 'polyline,travelSummary',
11-
units: 'imperial',
12-
spans: 'truckAttributes'
13-
},
14-
routes = new H.map.Group();
5+
const routeRequestParams = {
6+
routingMode: "fast",
7+
transportMode: "truck",
8+
origin: "40.7249546323,-74.0110042", // Manhattan
9+
destination: "40.7324386599,-74.0341396", // Newport
10+
return: "polyline",
11+
units: "imperial",
12+
spans: "truckAttributes",
13+
},
14+
routes = new H.map.Group();
1515

1616
function calculateRoutes(platform) {
17-
var router = platform.getRoutingService(null, 8);
17+
const router = platform.getRoutingService(null, 8);
1818

1919
// The blue route showing a simple truck route
2020
calculateRoute(router, routeRequestParams, {
21-
strokeColor: 'rgba(0, 128, 255, 0.7)',
21+
strokeColor: "rgba(0, 128, 255, 0.7)",
2222
lineWidth: 10
2323
});
2424

25-
// The green route showing a truck route with a trailer
26-
calculateRoute(router, Object.assign(routeRequestParams, {
27-
'truck[axleCount]': 4,
28-
}), {
29-
strokeColor: 'rgba(25, 150, 10, 0.7)',
30-
lineWidth: 7
31-
});
25+
// The fuchsia route showing a truck route to transport flammable hazardous goods
26+
// with 4 axles and a trailer.
27+
calculateRoute(
28+
router,
29+
Object.assign({}, routeRequestParams, {
30+
"vehicle[axleCount]": 4,
31+
"vehicle[trailerCount]": 1,
32+
"vehicle[hazardousGoods]": "flammable"
33+
}),
34+
{
35+
strokeColor: "rgba(255, 0, 255, 0.7)",
36+
lineWidth: 10
37+
}
38+
);
39+
}
3240

33-
// The violet route showing a truck route with a trailer
34-
calculateRoute(router, Object.assign(routeRequestParams, {
35-
'truck[axleCount]': 5,
36-
'truck[shippedHazardousGoods]': 'flammable'
37-
}), {
38-
strokeColor: 'rgba(255, 0, 255, 0.7)',
39-
lineWidth: 5
40-
});
41+
// Event handler used to enable vehicle restrictions on style load.
42+
function enableVehicleRestrictions(event) {
43+
// Check the style state.
44+
if (event.target.getState() === H.map.render.Style.State.READY) {
45+
// Remove the attached event listener.
46+
event.target.removeEventListener(
47+
"change",
48+
enableVehicleRestrictions,
49+
false
50+
);
51+
const features = event.target.getEnabledFeatures();
52+
// Enable vehicle restrictions feature in "active & inactive" mode.
53+
event.target.setEnabledFeatures([
54+
...features,
55+
{ feature: "vehicle restrictions", mode: "active & inactive" },
56+
]);
57+
}
4158
}
4259

4360
/**
@@ -46,57 +63,74 @@ function calculateRoutes(platform) {
4663
* @param {H.service.RoutingService} router The service stub for requesting the Routing API
4764
* @param {mapsjs.map.SpatialStyle.Options} style The style of the route to display on the map
4865
*/
49-
function calculateRoute (router, params, style) {
50-
router.calculateRoute(params, function(result) {
51-
addRouteShapeToMap(style, result.routes[0]);
52-
}, console.error);
66+
function calculateRoute(router, params, style) {
67+
router.calculateRoute(
68+
params,
69+
function (result) {
70+
addRouteShapeToMap(style, result.routes[0]);
71+
},
72+
console.error
73+
);
5374
}
5475

5576
/**
5677
* Boilerplate map initialization code starts below:
5778
*/
5879

5980
// set up containers for the map + panel
60-
var mapContainer = document.getElementById('map');
81+
const mapContainer = document.getElementById("map");
6182

6283
// Step 1: initialize communication with the platform
6384
// In your own code, replace variable window.apikey with your own apikey
64-
var platform = new H.service.Platform({
85+
const platform = new H.service.Platform({
6586
apikey: window.apikey
6687
});
6788

68-
var defaultLayers = platform.createDefaultLayers();
89+
// Step 2: Specify the rendering engine.
90+
// In this example, we use the HARP rendering engine.
91+
// Note: HARP is not the default engine and is not included in mapsjs-core.js.
92+
// To use HARP, ensure you include the mapsjs-harp.js script in your HTML.
93+
const engineType = H.Map.EngineType["HARP"];
6994

70-
// Step 2: initialize a map - this map is centered over Berlin
71-
var map = new H.Map(mapContainer,
72-
// Set truck restriction layer as a base map
73-
defaultLayers.vector.normal.truck,{
74-
center: {lat: 40.745390, lng: -74.022917},
95+
// Step 3: Create default map layers using the HARP engine.
96+
const defaultLayers = platform.createDefaultLayers({ engineType });
97+
98+
// Step 4: Initialize the map using the HARP engine.
99+
const map = new H.Map(mapContainer, defaultLayers.vector.normal.logistics, {
100+
engineType,
101+
center: { lat: 40.74539, lng: -74.022917 },
75102
zoom: 13.2,
76103
pixelRatio: window.devicePixelRatio || 1
77104
});
105+
const style = map.getBaseLayer().getProvider().getStyle();
106+
107+
// Step 5: Enable vehicle restrictions display on the map.
108+
style.addEventListener("change", enableVehicleRestrictions);
109+
110+
78111
// add a resize listener to make sure that the map occupies the whole container
79-
window.addEventListener('resize', () => map.getViewPort().resize());
112+
window.addEventListener("resize", () => map.getViewPort().resize());
80113

81-
// Step 3: make the map interactive
82-
// MapEvents enables the event system
83-
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
84-
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
114+
// Step 6: Enable map interactivity.
115+
// MapEvents enables the map's event system.
116+
// Behavior enables default user interactions such as pan and zoom,
117+
// including support for touch gestures on mobile devices.
118+
const behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
85119

86120
map.addObject(routes);
87121

88122
/**
89123
* Creates a H.map.Polyline from the shape of the route and adds it to the map.
90124
* @param {Object} route A route as received from the H.service.RoutingService
91125
*/
92-
function addRouteShapeToMap(style, route){
126+
function addRouteShapeToMap(style, route) {
93127
route.sections.forEach((section) => {
94128
// decode LineString from the flexible polyline
95-
let linestring = H.geo.LineString.fromFlexiblePolyline(section.polyline);
129+
const linestring = H.geo.LineString.fromFlexiblePolyline(section.polyline);
96130

97131
// Create a polyline to display the route:
98-
let polyline = new H.map.Polyline(linestring, {
99-
style: style
132+
const polyline = new H.map.Polyline(linestring, {
133+
style
100134
});
101135

102136
// Add the polyline to the map
@@ -109,4 +143,4 @@ function addRouteShapeToMap(style, route){
109143
}
110144

111145
// Now use the map as required...
112-
calculateRoutes (platform);
146+
calculateRoutes(platform);

0 commit comments

Comments
 (0)