-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateRoutes.js
78 lines (74 loc) · 2.74 KB
/
calculateRoutes.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
const baseUrlCombustion = "https://api.tomtom.com/routing/1/calculateRoute";
const baseUrlEV =
"https://api.tomtom.com/routing/1/calculateLongDistanceEVRoute";
const traffic = false;
function calculateRoutes(start_lat, start_long, dest_lat, dest_long) {
const coordinates = `${start_lat},${start_long}:${dest_lat},${dest_long}`;
const key = localStorage.getItem("API-Key");
if (key.match("[a-zA-Z0-9]{30,35}")) {
if (vehicleData.length == 0) {
window.alert("Please enter vehicleData suitable for routing to use this feature.");
return;
}
calculateCombustionRoute(coordinates, key);
calculateEVRoutes(coordinates, key);
} else if (key == ""){
window.alert("Make sure to enter a valid API-Key to use this feature.");
} else {
window.alert(
`Invalid API-Key: "${key}". If you are sure, that your key is correct, notify us to adapt the regex for evaluating.`
);
}
}
function calculateCombustionRoute(coordinates, key) {
const url = `${baseUrlCombustion}/${coordinates}/json?\
maxAlternatives=0&language=de-DE\
&computeBestOrder=false\
&routeRepresentation=polyline\
&computeTravelTimeFor=all\
§ionType=travelMode\
&departAt=now\
&routeType=fastest\
&traffic=${traffic}\
&avoid=unpavedRoads\
&travelMode=car\
&vehicleCommercial=false\
&vehicleEngineType=combustion\
&key=${key}`;
httpGetAsync(url, displayAPIResult, { name: "Combustion" });
}
function calculateEVRoutes(coordinates, key) {
vehicleData.forEach((vehicle) => {
const body = JSON.stringify(vehicle.body);
const url = `${baseUrlEV}/${coordinates}/json?\
vehicleEngineType=electric&avoid=unpavedRoads\
&traffic=${traffic}\
&constantSpeedConsumptionInkWhPerHundredkm=${vehicle.constantSpeedConsumption}\
¤tChargeInkWh=${vehicle.currentChargeInkWh.toFixed(2)}\
&maxChargeInkWh=${vehicle.maxCharge}\
&minChargeAtDestinationInkWh=${vehicle.minChargeAtDestinationInkWh.toFixed(2)}\
&minChargeAtChargingStopsInkWh=${vehicle.minChargeAtChargingStopsInkWh.toFixed(
2
)}\
&vehicleWeight=${vehicle.weight}\
&uphillEfficiency=${vehicle.uphillEfficiency}\
&downhillEfficiency=${vehicle.downhillEfficiency}\
&auxiliaryPowerInkW=${vehicle.auxiliaryPower}\
&key=${key}`;
httpPostAsync(url, body, displayAPIResult, vehicle);
});
}
function displayAPIResult(response, vehicleInfo) {
const json = JSON.parse(response);
const route = json.routes[0];
const legs = route.legs;
const summary = route.summary;
const latlngs = [];
legs.forEach((leg) => {
const points = leg.points;
points.forEach((point) => {
latlngs.push([point.latitude, point.longitude]);
});
});
displayRoute(latlngs, summary, vehicleInfo);
}