-
Notifications
You must be signed in to change notification settings - Fork 65
/
routing.js
82 lines (62 loc) · 2.19 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
70
71
72
73
74
75
76
77
78
79
80
81
82
// Get an instance of the routing service version 7:
var router = platform.getRoutingService();
var routingParameters = {
waypoint0:"52.53086235,13.38475371",// Berlin, Germany
waypoint1:"53.13256,17.98909",// Warsaw, Poland
mode:"fastest;truck;traffic:enabled",
representation: "display",
routeAttributes:"-summary",
legAttributes:'none',
maneuverAttributes:"none"
};
// Define a callback function to process the routing response:
var onResult = function(result) {
console.log(result);
var colors =["green","orange","yellow","red"]
result.response.route.forEach(route =>{
var lineString = new H.geo.LineString(),
routeShape = route.shape,
polyline,
startMarker,
endMarker;
// Retrieve the mapped positions of the requested waypoints:
startPoint = route.waypoint[0].mappedPosition;
endPoint = route.waypoint[1].mappedPosition;
routeShape.forEach(function(point) {
var parts = point.split(',');
lineString.pushLatLngAlt(parts[0], parts[1]);
});
polyline = new H.map.Polyline(lineString, {
style: {
lineWidth: 4,
strokeColor: colors[result.response.route.indexOf(route)]
}
});
startMarker = new H.map.Marker({
lat:route.waypoint[0].mappedPosition.latitude,
lng:route.waypoint[0].mappedPosition.longitude
},{icon:(new H.map.Icon('img/truck.png'))});
endMarker = new H.map.Marker({
lat:route.waypoint[1].mappedPosition.latitude,
lng:route.waypoint[1].mappedPosition.longitude
},{icon:(new H.map.Icon('img/end.png'))});
// Add the polyline to the map
map.addObjects([polyline,startMarker,endMarker]);
if(route.summaryByCountry){
route.summaryByCountry.forEach(sc=>{
// console.log(sc.text);
document.getElementById("panel").innerHTML += sc.country +': '+ sc.text + `<br>`;
});
}
// And zoom to its bounding rectangle
map.getViewPort().setPadding(100, 0, 0, 0);
map.getViewModel().setLookAtData({
bounds: polyline.getBoundingBox()
});
map.getViewPort().setPadding(0, 0, 0, 0);
});
};
var onError = function(error) {
alert(error.message);
};
router.calculateRoute(routingParameters, onResult, onError);