-
Notifications
You must be signed in to change notification settings - Fork 7
/
routing.js
54 lines (42 loc) · 1.69 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
// 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',
alternatives:3,
return:'polyline',
};
// Define a callback function to process the routing response:
var onResult = function(result) {
console.log(result);
if (result.routes.length) {
result.routes.forEach(route =>{
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);
// 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()});
});
});
}
};
var onError = function(error) {
alert(error.message);
};
// Call calculateRoute() with the routing parameters,
// the callback and an error callback function (called if a
// communication error occurs):
router.calculateRoute(routingParameters, onResult, onError);