Skip to content

Commit c2ba6b1

Browse files
author
Konstantin Plotnikov
committed
Changed routing example to use Routing v8
Signed-off-by: Konstantin Plotnikov <[email protected]>
1 parent 48f7783 commit c2ba6b1

File tree

6 files changed

+183
-474
lines changed

6 files changed

+183
-474
lines changed

map-with-pedestrian-route-from-a-to-b/demo.html

Lines changed: 3 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -26,142 +26,15 @@
2626
.directions li span.depart {
2727
background-position:-28px;
2828
}
29-
.directions li span.rightUTurn {
30-
background-position:-56px;
31-
}
32-
.directions li span.leftUTurn {
33-
background-position:-84px;
34-
}
35-
.directions li span.rightFork {
36-
background-position:-112px;
37-
}
38-
.directions li span.leftFork {
39-
background-position:-140px;
40-
}
41-
.directions li span.rightMerge {
42-
background-position:-112px;
43-
}
44-
.directions li span.leftMerge {
45-
background-position:-140px;
46-
}
47-
.directions li span.slightRightTurn {
48-
background-position:-168px;
49-
}
50-
.directions li span.slightLeftTurn{
51-
background-position:-196px;
52-
}
53-
.directions li span.rightTurn {
29+
.directions li span.rightturn {
5430
background-position:-224px;
5531
}
56-
.directions li span.leftTurn{
32+
.directions li span.leftturn{
5733
background-position:-252px;
5834
}
59-
.directions li span.sharpRightTurn {
60-
background-position:-280px;
61-
}
62-
.directions li span.sharpLeftTurn{
63-
background-position:-308px;
64-
}
65-
.directions li span.rightRoundaboutExit1 {
66-
background-position:-616px;
67-
}
68-
.directions li span.rightRoundaboutExit2 {
69-
background-position:-644px;
70-
}
71-
72-
.directions li span.rightRoundaboutExit3 {
73-
background-position:-672px;
74-
}
75-
76-
.directions li span.rightRoundaboutExit4 {
77-
background-position:-700px;
78-
}
79-
80-
.directions li span.rightRoundaboutPass {
81-
background-position:-700px;
82-
}
83-
84-
.directions li span.rightRoundaboutExit5 {
85-
background-position:-728px;
86-
}
87-
.directions li span.rightRoundaboutExit6 {
88-
background-position:-756px;
89-
}
90-
.directions li span.rightRoundaboutExit7 {
91-
background-position:-784px;
92-
}
93-
.directions li span.rightRoundaboutExit8 {
94-
background-position:-812px;
95-
}
96-
.directions li span.rightRoundaboutExit9 {
97-
background-position:-840px;
98-
}
99-
.directions li span.rightRoundaboutExit10 {
100-
background-position:-868px;
101-
}
102-
.directions li span.rightRoundaboutExit11 {
103-
background-position:896px;
104-
}
105-
.directions li span.rightRoundaboutExit12 {
106-
background-position:924px;
107-
}
108-
.directions li span.leftRoundaboutExit1 {
109-
background-position:-952px;
110-
}
111-
.directions li span.leftRoundaboutExit2 {
112-
background-position:-980px;
113-
}
114-
.directions li span.leftRoundaboutExit3 {
115-
background-position:-1008px;
116-
}
117-
.directions li span.leftRoundaboutExit4 {
118-
background-position:-1036px;
119-
}
120-
.directions li span.leftRoundaboutPass {
121-
background-position:1036px;
122-
}
123-
.directions li span.leftRoundaboutExit5 {
124-
background-position:-1064px;
125-
}
126-
.directions li span.leftRoundaboutExit6 {
127-
background-position:-1092px;
128-
}
129-
.directions li span.leftRoundaboutExit7 {
130-
background-position:-1120px;
131-
}
132-
.directions li span.leftRoundaboutExit8 {
133-
background-position:-1148px;
134-
}
135-
.directions li span.leftRoundaboutExit9 {
136-
background-position:-1176px;
137-
}
138-
.directions li span.leftRoundaboutExit10 {
139-
background-position:-1204px;
140-
}
141-
.directions li span.leftRoundaboutExit11 {
142-
background-position:-1232px;
143-
}
144-
.directions li span.leftRoundaboutExit12 {
145-
background-position:-1260px;
146-
}
14735
.directions li span.arrive {
14836
background-position:-1288px;
14937
}
150-
.directions li span.leftRamp {
151-
background-position:-392px;
152-
}
153-
.directions li span.rightRamp {
154-
background-position:-420px;
155-
}
156-
.directions li span.leftExit {
157-
background-position:-448px;
158-
}
159-
.directions li span.rightExit {
160-
background-position:-476px;
161-
}
162-
.directions li span.ferry {
163-
background-position:-1316px;
164-
}
16538
</style>
16639
</head>
16740
<body id="markers-on-the-map">
@@ -179,7 +52,7 @@ <h3>Code</h3>
17952
<p>Access to the routing service is obtained from the <code>H.service.Platform</code> by calling
18053
<code>getRoutingService()</code>. The <code>calculateRoute()</code> method is used to calculate the shortest
18154
pedestrian (<code>mode:shortest;pedestrian</code>) route by passing in the relevant parameters as defined in
182-
<a href="http://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-route.html" target="_blank">Routing API</a>.
55+
<a href="https://developer.here.com/documentation/routing-api/dev_guide/topics/use-cases/calculate-route.html" target="_blank">Routing API</a>.
18356
The styling and display of the response is under the control of the developer.</p>
18457
<script type="text/javascript" src='demo.js'></script>
18558
</body>

map-with-pedestrian-route-from-a-to-b/demo.js

Lines changed: 63 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
* @param {H.service.Platform} platform A stub class to access HERE services
99
*/
1010
function calculateRouteFromAtoB (platform) {
11-
var router = platform.getRoutingService(),
12-
routeRequestParams = {
13-
mode: 'shortest;pedestrian',
14-
representation: 'display',
15-
waypoint0: '51.51326,-0.0968752', // St Paul's Cathedral
16-
waypoint1: '51.5081,-0.0985', // Tate Modern
17-
routeattributes: 'waypoints,summary,shape,legs',
18-
maneuverattributes: 'direction,action'
19-
};
11+
var router = platform.getRoutingService(null, 8),
12+
routeRequestParams = {
13+
routingMode: 'fast',
14+
transportMode: 'pedestrian',
15+
origin: '51.51326,-0.0968752', // St Paul's Cathedral
16+
destination: '51.5081,-0.0985', // Tate Modern
17+
return: 'polyline,turnByTurnActions,actions,instructions,travelSummary'
18+
};
2019

2120

2221
router.calculateRoute(
@@ -32,18 +31,16 @@ function calculateRouteFromAtoB (platform) {
3231
* see: http://developer.here.com/rest-apis/documentation/routing/topics/resource-type-calculate-route.html
3332
*/
3433
function onSuccess(result) {
35-
var route = result.response.route[0];
34+
var route = result.routes[0];
3635
/*
3736
* The styling of the route response on the map is entirely under the developer's control.
3837
* A representitive styling can be found the full JS + HTML code of this example
3938
* in the functions below:
4039
*/
4140
addRouteShapeToMap(route);
4241
addManueversToMap(route);
43-
44-
addWaypointsToPanel(route.waypoint);
4542
addManueversToPanel(route);
46-
addSummaryToPanel(route.summary);
43+
addSummaryToPanel(route);
4744
// ... etc.
4845
}
4946

@@ -116,26 +113,24 @@ function openBubble(position, text){
116113
* @param {Object} route A route as received from the H.service.RoutingService
117114
*/
118115
function addRouteShapeToMap(route){
119-
var lineString = new H.geo.LineString(),
120-
routeShape = route.shape,
121-
polyline;
122-
123-
routeShape.forEach(function(point) {
124-
var parts = point.split(',');
125-
lineString.pushLatLngAlt(parts[0], parts[1]);
126-
});
127-
128-
polyline = new H.map.Polyline(lineString, {
129-
style: {
130-
lineWidth: 4,
131-
strokeColor: 'rgba(0, 128, 255, 0.7)'
132-
}
133-
});
134-
// Add the polyline to the map
135-
map.addObject(polyline);
136-
// And zoom to its bounding rectangle
137-
map.getViewModel().setLookAtData({
138-
bounds: polyline.getBoundingBox()
116+
route.sections.forEach((section) => {
117+
// decode LineString from the flexible polyline
118+
let linestring = H.geo.LineString.fromFlexiblePolyline(section.polyline);
119+
120+
// Create a polyline to display the route:
121+
let polyline = new H.map.Polyline(linestring, {
122+
style: {
123+
lineWidth: 4,
124+
strokeColor: 'rgba(0, 128, 255, 0.7)'
125+
}
126+
});
127+
128+
// Add the polyline to the map
129+
map.addObject(polyline);
130+
// And zoom to its bounding rectangle
131+
map.getViewModel().setLookAtData({
132+
bounds: polyline.getBoundingBox()
133+
});
139134
});
140135
}
141136

@@ -154,65 +149,50 @@ function addManueversToMap(route){
154149
group = new H.map.Group(),
155150
i,
156151
j;
152+
route.sections.forEach((section) => {
153+
let poly = H.geo.LineString.fromFlexiblePolyline(section.polyline).getLatLngAltArray();
157154

158-
// Add a marker for each maneuver
159-
for (i = 0; i < route.leg.length; i += 1) {
160-
for (j = 0; j < route.leg[i].maneuver.length; j += 1) {
161-
// Get the next maneuver.
162-
maneuver = route.leg[i].maneuver[j];
163-
// Add a marker to the maneuvers group
155+
let actions = section.actions;
156+
// Add a marker for each maneuver
157+
for (i = 0; i < actions.length; i += 1) {
158+
let action = actions[i];
164159
var marker = new H.map.Marker({
165-
lat: maneuver.position.latitude,
166-
lng: maneuver.position.longitude} ,
160+
lat: poly[action.offset * 3],
161+
lng: poly[action.offset * 3 + 1]},
167162
{icon: dotIcon});
168-
marker.instruction = maneuver.instruction;
163+
marker.instruction = action.instruction;
169164
group.addObject(marker);
170165
}
171-
}
172166

173-
group.addEventListener('tap', function (evt) {
174-
map.setCenter(evt.target.getGeometry());
175-
openBubble(
176-
evt.target.getGeometry(), evt.target.instruction);
177-
}, false);
167+
group.addEventListener('tap', function (evt) {
168+
map.setCenter(evt.target.getGeometry());
169+
openBubble(
170+
evt.target.getGeometry(), evt.target.instruction);
171+
}, false);
178172

179-
// Add the maneuvers group to the map
180-
map.addObject(group);
173+
// Add the maneuvers group to the map
174+
map.addObject(group);
175+
});
181176
}
182177

183178

184179
/**
185180
* Creates a series of H.map.Marker points from the route and adds them to the map.
186181
* @param {Object} route A route as received from the H.service.RoutingService
187182
*/
188-
function addWaypointsToPanel(waypoints){
183+
function addSummaryToPanel(route){
184+
let duration = 0,
185+
distance = 0;
189186

187+
route.sections.forEach((section) => {
188+
distance += section.travelSummary.length;
189+
duration += section.travelSummary.duration;
190+
});
190191

191-
192-
var nodeH3 = document.createElement('h3'),
193-
waypointLabels = [],
194-
i;
195-
196-
197-
for (i = 0; i < waypoints.length; i += 1) {
198-
waypointLabels.push(waypoints[i].label)
199-
}
200-
201-
nodeH3.textContent = waypointLabels.join(' - ');
202-
203-
routeInstructionsContainer.innerHTML = '';
204-
routeInstructionsContainer.appendChild(nodeH3);
205-
}
206-
207-
/**
208-
* Creates a series of H.map.Marker points from the route and adds them to the map.
209-
* @param {Object} route A route as received from the H.service.RoutingService
210-
*/
211-
function addSummaryToPanel(summary){
212192
var summaryDiv = document.createElement('div'),
213193
content = '';
214-
content += '<b>Total distance</b>: ' + summary.distance + 'm. <br/>';
215-
content += '<b>Travel Time</b>: ' + summary.travelTime.toMMSS() + ' (in current traffic)';
194+
content += '<b>Total distance</b>: ' + distance + 'm. <br/>';
195+
content += '<b>Travel Time</b>: ' + duration.toMMSS();
216196

217197

218198
summaryDiv.style.fontSize = 'small';
@@ -227,36 +207,27 @@ function addSummaryToPanel(summary){
227207
* @param {Object} route A route as received from the H.service.RoutingService
228208
*/
229209
function addManueversToPanel(route){
230-
231-
232-
233-
var nodeOL = document.createElement('ol'),
234-
i,
235-
j;
210+
var nodeOL = document.createElement('ol');
236211

237212
nodeOL.style.fontSize = 'small';
238213
nodeOL.style.marginLeft ='5%';
239214
nodeOL.style.marginRight ='5%';
240215
nodeOL.className = 'directions';
241216

242-
// Add a marker for each maneuver
243-
for (i = 0; i < route.leg.length; i += 1) {
244-
for (j = 0; j < route.leg[i].maneuver.length; j += 1) {
245-
// Get the next maneuver.
246-
maneuver = route.leg[i].maneuver[j];
247-
217+
route.sections.forEach((section) => {
218+
section.actions.forEach((action, idx) => {
248219
var li = document.createElement('li'),
249-
spanArrow = document.createElement('span'),
250-
spanInstruction = document.createElement('span');
220+
spanArrow = document.createElement('span'),
221+
spanInstruction = document.createElement('span');
251222

252-
spanArrow.className = 'arrow ' + maneuver.action;
253-
spanInstruction.innerHTML = maneuver.instruction;
223+
spanArrow.className = 'arrow ' + (action.direction || '') + action.action;
224+
spanInstruction.innerHTML = section.actions[idx].instruction;
254225
li.appendChild(spanArrow);
255226
li.appendChild(spanInstruction);
256227

257228
nodeOL.appendChild(li);
258-
}
259-
}
229+
});
230+
});
260231

261232
routeInstructionsContainer.appendChild(nodeOL);
262233
}

0 commit comments

Comments
 (0)