-
Notifications
You must be signed in to change notification settings - Fork 65
/
routing.js
69 lines (49 loc) · 2.15 KB
/
routing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Get an instance of the routing service version 8:
var router = platform.getRoutingService(null, 8);
// Create the parameters for the routing request:
var routingParameters = {
transportMode:'car',
routingMode: 'fast',
origin: '52.4569927,13.380545',
destination: '52.52407865,13.429371',
return:'polyline,summary',
};
// Define a callback function to process the routing response:
var onResult = function(result) {
console.log(result);
if (result.routes.length) {
result.routes.forEach(route =>{
let totalLength = 0;
let totalDuration = 0;
route.sections.forEach((section) => {
// Create a linestring to use as a point source for the route line
let linestring = H.geo.LineString.fromFlexiblePolyline(section.polyline);
// Create a polyline to display the route:
let routeLine = new H.map.Polyline(linestring, {
style: { strokeColor: '#034F84', lineWidth: 3 }
});
// Create a marker for the start point:
let startMarker = new H.map.Marker(section.departure.place.location);
// Create a marker for the end point:
let endMarker = new H.map.Marker(section.arrival.place.location);
totalLength += section.summary.length;
totalDuration += section.summary.duration;
// Add the route polyline and the two markers to the map:
map.addObjects([routeLine, startMarker, endMarker]);
// Set the map's viewport to make the whole route visible:
map.getViewModel().setLookAtData({bounds: routeLine.getBoundingBox()});
});
document.getElementById("panel").innerHTML += 'Route '+(result.routes.indexOf(route)+1)+ ' Distance: '+ totalLength/1000 +' Km'+' Duration: '+ totalDuration.toMMSS() + `<br>`;
});
}
};
var onError = function(error) {
alert(error.message);
};
Number.prototype.toMMSS = function () {
return Math.floor(this / 60) +' minutes '+ (this % 60) + ' seconds.';
}
// Call calculateRoute() with the routing parameters,
// the callback and an error callback function (called if a
// communication error occurs):
router.calculateRoute(routingParameters, onResult, onError);