-
Notifications
You must be signed in to change notification settings - Fork 2
/
A_B_Routing.js
128 lines (108 loc) · 3.86 KB
/
A_B_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
$(document).ready(function () {
const api_key = "YOUR_API_KEY";
var startPosition = null;
var destinationPosition = null;
// This function decorates the route calculation by setting markers at the start and destination at the map.
function setMarkers() {
// Here the old markers of the previous calculation are deleted.
removeAllMarkers(map);
// The new markers are set.
L.marker(startPosition).addTo(map);
L.marker(destinationPosition).addTo(map);
}
function geocodeAdress(searchText, action) {
// Define the parameters needed for the REST query.
fetch(`https://api.myptv.com/geocoding/v1/locations/by-text?searchText=${searchText}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"apiKey": api_key
}
})
.then(response => response.json()
.then(result => {
action(result.locations[0].referencePosition);
}))
.catch(ex => {
alert(ex.message);
});
}
function fetchRoute() {
fetch(
`https://api.myptv.com/routing/v1/routes?waypoints=${startPosition[0].toString()},${startPosition[1].toString()}&waypoints=${destinationPosition[0].toString()},${destinationPosition[1].toString()}&results=POLYLINE&options[trafficMode]=AVERAGE`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"apiKey": api_key
}
})
.then(response => response.json()
.then(result => {
document.getElementById("travelResult").innerHTML = convertTime(result.travelTime) + ' for ' + convertDistance(result.distance);
displayPolyline(map, JSON.parse(result.polyline));
}))
.catch(ex => {
alert(ex.message);
});
}
$("#btnSubmit").click(function () {
try {
geocodeAdress($("#startFormId").val(), (coord) => {
// Extract the start position from the response.
startPosition = [coord.latitude, coord.longitude];
geocodeAdress($("#destinationFormId").val(), (coord) => {
// Extract the start position from the response.
destinationPosition = [coord.latitude, coord.longitude];
setMarkers();
// This function calls the Routing API to get the calculated route.
fetchRoute();
});
});
} catch (ex) {
alert(ex.message);
}
});
var coordinate = L.latLng(49, 8.4);
var map = new L.Map('map', {
center: coordinate,
zoom: 13,
zoomControl: false
});
L.control.zoom({
position: 'bottomright'
}).addTo(map);
var tileLayer = new L.tileLayer(
"https://api.myptv.com/rastermaps/v1/image-tiles/{z}/{x}/{y}?size={tileSize}",
{
attribution: "© " + new Date().getFullYear() + ", PTV Logistics, HERE",
tileSize: 256,
trackResize: false,
},
[
{ header: "apiKey", value: api_key },
]).addTo(map);
});
function removeAllMarkers(map) {
map.eachLayer((layer) => {
if (layer instanceof L.Marker) {
layer.remove();
}
});
}
var polylineLayer = null;
function displayPolyline(map, poly) {
if (polylineLayer !== null) {
map.removeLayer(polylineLayer);
}
var myStyle = {
"color": '#2882C8',
"weight": 5,
"opacity": 0.65
};
polylineLayer = L.geoJSON(poly, {
style: myStyle
}).addTo(map);
map.fitBounds(polylineLayer.getBounds());
}