-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
420 lines (367 loc) · 17 KB
/
script.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Function to calculate route distance using OpenStreetMap
async function calculateRouteDistanceOpenStreetMap(lat1, lon1, lat2, lon2) {
const osmUrl = `https://router.project-osrm.org/route/v1/driving/${lon1},${lat1};${lon2},${lat2}?overview=false`;
const response = await fetch(osmUrl);
const data = await response.json();
if (data.routes && data.routes.length > 0) {
return data.routes[0].distance;
} else {
throw new Error('Unable to process route');
}
}
// Function to find nearest parkings using OpenStreetMap
async function findNearestParkingsOpenStreetMap(userLat, userLon, parkings) {
const parkingDistances = await Promise.all(parkings.map(async (parking) => {
const parkingLat = parking.geo_point_2d.lat;
const parkingLon = parking.geo_point_2d.lon;
try {
const distance = await calculateRouteDistanceOpenStreetMap(userLat, userLon, parkingLat, parkingLon);
return { ...parking, distance };
} catch (error) {
console.error('Error calculating distance:', error);
return { ...parking, distance: null };
}
}));
parkingDistances.sort((a, b) => (a.distance !== null && b.distance !== null) ? a.distance - b.distance : (a.distance === null) ? 1 : -1);
return parkingDistances.filter(parking => parking.distance !== null).slice(0, 5);
}
function displayParkings(parkings, userLat, userLon) {
document.getElementById('loadingSpinner').style.display = 'none';
const parkingList = document.getElementById('parkingList');
parkingList.innerHTML = ''; // Opschonen van de parkeerlijst
document.getElementById('backButton').style.display = 'inline-block'; // Toon de 'Terug'-knop
parkings.forEach(parking => {
// Het aanmaken van een parkeerelement
const div = document.createElement('div');
div.className = 'parking-item';
const websiteButton = parking.url ? `<a href="${parking.url}" target="_blank" class="btn btn-secondary">Website</a>` : '';
// Het aanmaken van een parkeerelement
div.innerHTML = `
<h2>${parking.naam}</h2>
<p>Adres: ${parking.straatnaam} ${parking.huisnr || ''}</p>
<p>Capaciteit: ${parking.capaciteit || 'Niet beschikbaar'}</p>
<p>Afstand: ${(parking.distance / 1000).toFixed(2)} km</p>
<div>${websiteButton}</div>
<button onclick="copyToClipboard('${parking.geo_point_2d.lat}, ${parking.geo_point_2d.lon}')" class="btn btn-primary">Coördinaten kopiëren</button>
`;
// Het creëren en toevoegen van de knop 'Openen in Google Maps
const openMapButton = document.createElement('button');
openMapButton.textContent = 'Open in Google Maps';
openMapButton.classList.add('btn', 'btn-secondary');
openMapButton.onclick = () => openInMaps(parking.geo_point_2d.lat, parking.geo_point_2d.lon);
div.appendChild(openMapButton);
// Het creëren en toevoegen van de knop 'Route plannen
const routeButton = document.createElement('button');
routeButton.textContent = 'Route plannen';
routeButton.classList.add('btn', 'btn-primary');
routeButton.onclick = () => openRouteInMaps(userLat, userLon, parking.geo_point_2d.lat, parking.geo_point_2d.lon);
div.appendChild(routeButton);
// Het creëren en toevoegen van de knop 'Route plannen
const qrCodeElement = document.createElement('div');
qrCodeElement.classList.add('qr-code-container');
div.appendChild(qrCodeElement);
// Genereren en toevoegen van een QR-code in de container
new QRCode(qrCodeElement, {
text: `https://www.google.com/maps?q=${parking.geo_point_2d.lat},${parking.geo_point_2d.lon}`,
width: 128,
height: 128,
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
});
// Het toevoegen van de gehele blok aan de lijst op de pagina
parkingList.appendChild(div);
});
}
function openInMaps(parkingLat, parkingLon) {
if (typeof parkingLat !== "number" || typeof parkingLon !== "number") {
console.error("Ongeldige coördinaten voor het weergeven van de kaart.");
return;
}
const location = encodeURIComponent(`${parkingLat},${parkingLon}`);
const url = `https://www.google.com/maps?q=${location}`;
window.open(url, '_blank');
}
function openRouteInMaps(userLat, userLon, parkingLat, parkingLon) {
// Ervoor zorgen dat alle parameters getallen zijn
if (typeof userLat !== "number" || typeof userLon !== "number" ||
typeof parkingLat !== "number" || typeof parkingLon !== "number") {
console.error("Onjuiste coördinaten voor routeplanning.");
return;
}
//Een URL aanmaken met deze parameters
const origin = encodeURIComponent(`${userLat},${userLon}`);
const destination = encodeURIComponent(`${parkingLat},${parkingLon}`);
const url = `https://www.google.com/maps/dir/?api=1&origin=${origin}&destination=${destination}&travelmode=driving`;
window.open(url, '_blank');
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
alert('Coördinaten gekopieerd naar klembord');
}).catch(err => {
console.error('Fout tijdens het kopiëren: ', err);
});
}
document.getElementById('findParking').addEventListener('click', function() {
document.getElementById('loadingSpinner').style.display = 'block';
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(async function(position) {
const userLat = position.coords.latitude;
const userLon = position.coords.longitude;
try {
const response = await fetch('https://data.stad.gent/api/v2/catalog/datasets/locaties-openbare-parkings-gent/exports/json');
const parkings = await response.json();
const nearestParkings = await findNearestParkingsOpenStreetMap(userLat, userLon, parkings);
displayParkings(nearestParkings, userLat, userLon);
} catch (error) {
console.error('Fout bij het aanvragen van de API:', error);
}
}, function(error) {
console.error('Fout bij ophalen van geolocatie:', error);
});
} else {
alert('Geolocatie wordt niet ondersteund door uw browser');
}
});
document.getElementById('backButton').addEventListener('click', function() {
this.style.display = 'none'; // Knop "terug" verbergen
document.getElementById('parkingList').innerHTML = ''; //Lijst met parkeerplaatsen wissen
document.getElementById('addressInput').value = ''; // Adresinvoer wissen
});
// Functie toevoegen voor het geocoderen van een adres
function geocodeAddress(address) {
const nominatimUrl = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(address)}`;
return fetch(nominatimUrl)
.then(response => response.json())
.then(data => {
if (data.length > 0) {
return { lat: parseFloat(data[0].lat), lon: parseFloat(data[0].lon) };
} else {
throw new Error('Geen resultaten gevonden voor dit adres');
}
});
}
// Eventhandler voor de knop voor adreszoeken
document.getElementById('findParkingByAddress').addEventListener('click', function() {
document.getElementById('loadingSpinner').style.display = 'block';
const address = document.getElementById('addressInput').value;
if (address) {
geocodeAddress(address)
.then(coords => {
fetch('https://data.stad.gent/api/v2/catalog/datasets/locaties-openbare-parkings-gent/exports/json')
.then(response => response.json())
.then(parkings => {
return findNearestParkingsOpenStreetMap(coords.lat, coords.lon, parkings);
})
.then(nearestParkings => {
displayParkings(nearestParkings, coords.lat, coords.lon);
})
.catch(error => {
console.error('Fout bij het aanvragen van de API:', error);
});
})
.catch(error => {
alert('Fout bij het geocoding: ' + error.message);
});
} else {
alert('Voer een geldig adres in.');
}
});
//Dynamically datasets.Cards//
//Percentage scale//
fetch('https://data.stad.gent/api/explore/v2.1/catalog/datasets/bezetting-parkeergarages-real-time/records?where=categorie%3D%22parking%20in%20LEZ%22&fields=name,totalcapacity,availablecapacity,lastupdate,type,location,categorie&limit=20')
.then(response => response.json())
.then(data => {
const progressBar = document.getElementById('progress-bar-lez').querySelector('.progress');
if (data.results && data.results.length > 0) {
let totalCapacity = 0;
let totalAvailable = 0;
data.results.forEach(record => {
totalCapacity += record.totalcapacity;
totalAvailable += record.availablecapacity;
});
const occupancyRate = ((totalCapacity - totalAvailable) / totalCapacity) * 100;
progressBar.style.width = occupancyRate.toFixed(2) + '%';
} else {
progressBar.style.width = '0%';
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
document.getElementById('progress-bar-lez').querySelector('.progress').style.width = '0%';
});
fetch('https://data.stad.gent/api/explore/v2.1/catalog/datasets/bezetting-parkeergarages-real-time/records?where=categorie%3D%22parking%20buiten%20LEZ%22&fields=name,totalcapacity,availablecapacity,lastupdate,type,location,categorie&limit=20')
.then(response => response.json())
.then(data => {
const progressBar = document.getElementById('progress-bar-nolez').querySelector('.progress');
if (data.results && data.results.length > 0) {
let totalCapacity = 0;
let totalAvailable = 0;
data.results.forEach(record => {
totalCapacity += record.totalcapacity;
totalAvailable += record.availablecapacity;
});
const occupancyRate = ((totalCapacity - totalAvailable) / totalCapacity) * 100;
progressBar.style.width = occupancyRate.toFixed(2) + '%';
} else {
progressBar.style.width = '0%';
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
document.getElementById('progress-bar-lez').querySelector('.progress').style.width = '0%';
});
fetch('https://data.stad.gent/api/explore/v2.1/catalog/datasets/real-time-bezetting-pr-gent/records?limit=20')
.then(response => response.json())
.then(data => {
const progressBar = document.getElementById('progress-bar-pr').querySelector('.progress');
if (data.results && data.results.length > 0) {
let totalSpaces = 0;
let totalAvailable = 0;
data.results.forEach(record => {
totalSpaces += record.numberofspaces;
totalAvailable += record.availablespaces;
});
const occupancyRate = ((totalSpaces - totalAvailable) / totalSpaces) * 100;
progressBar.style.width = occupancyRate.toFixed(2) + '%';
} else {
progressBar.style.width = '0%';
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
document.getElementById('progress-bar-pr').querySelector('.progress').style.width = '0%';
});
//info//
//LEZ//
fetch('https://data.stad.gent/api/explore/v2.1/catalog/datasets/bezetting-parkeergarages-real-time/records?where=categorie%3D%22parking%20in%20LEZ%22&fields=name,totalcapacity,availablecapacity,lastupdate,type,location,categorie&limit=20')
.then(response => response.json())
.then(data => {
const totalCapacityElement = document.querySelector('.total_capacity');
const availableCapacityElement = document.querySelector('.available_capacity');
const lastUpdatedElement = document.querySelector('.last_updated');
if (data.results && data.results.length > 0) {
let totalCapacity = 0;
let totalAvailable = 0;
let latestUpdate;
data.results.forEach(record => {
totalCapacity += record.totalcapacity;
totalAvailable += record.availablecapacity;
const updateDate = new Date(record.lastupdate);
if (!latestUpdate || updateDate > latestUpdate) {
latestUpdate = updateDate;
}
});
totalCapacityElement.textContent = `Total capacity: ${totalCapacity}`;
availableCapacityElement.textContent = `Available capacity: ${totalAvailable}`;
if (latestUpdate) {
lastUpdatedElement.textContent = `Last updated: ${latestUpdate.toLocaleString('nl-BE', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
})}`;
}
} else {
totalCapacityElement.textContent = 'No data';
availableCapacityElement.textContent = 'No data';
lastUpdatedElement.textContent = 'No data';
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
totalCapacityElement.textContent = 'Error loading data.';
availableCapacityElement.textContent = 'Error loading data.';
lastUpdatedElement.textContent = 'Error loading data.';
});
//No LEZ//
fetch('https://data.stad.gent/api/explore/v2.1/catalog/datasets/bezetting-parkeergarages-real-time/records?where=categorie%3D%22parking%20buiten%20LEZ%22&fields=name,totalcapacity,availablecapacity,lastupdate,type,location,categorie&limit=20')
.then(response => response.json())
.then(data => {
const totalCapacityElement = document.querySelector('.total_capacity2');
const availableCapacityElement = document.querySelector('.available_capacity2');
const lastUpdatedElement = document.querySelector('.last_updated2');
if (data.results && data.results.length > 0) {
let totalCapacity = 0;
let totalAvailable = 0;
let latestUpdate;
data.results.forEach(record => {
totalCapacity += record.totalcapacity;
totalAvailable += record.availablecapacity;
const updateDate = new Date(record.lastupdate);
if (!latestUpdate || updateDate > latestUpdate) {
latestUpdate = updateDate;
}
});
totalCapacityElement.textContent = `Total capacity: ${totalCapacity}`;
availableCapacityElement.textContent = `Available capacity: ${totalAvailable}`;
if (latestUpdate) {
lastUpdatedElement.textContent = `Last updated: ${latestUpdate.toLocaleString('nl-BE', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
})}`;
}
} else {
totalCapacityElement.textContent = 'No data';
availableCapacityElement.textContent = 'No data';
lastUpdatedElement.textContent = 'No data';
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
totalCapacityElement.textContent = 'Error loading data.';
availableCapacityElement.textContent = 'Error loading data.';
lastUpdatedElement.textContent = 'Error loading data.';
});
//P+R//
fetch('https://data.stad.gent/api/explore/v2.1/catalog/datasets/real-time-bezetting-pr-gent/records?limit=20')
.then(response => response.json())
.then(data => {
const totalCapacityElement = document.querySelector('.total_capacity3');
const availableCapacityElement = document.querySelector('.available_capacity3');
const lastUpdatedElement = document.querySelector('.last_updated3');
if (data.results && data.results.length > 0) {
let totalSpaces = 0;
let totalAvailable = 0;
let latestUpdate;
data.results.forEach(record => {
totalSpaces += record.numberofspaces;
totalAvailable += record.availablespaces;
const updateDate = new Date(record.lastupdate);
if (!latestUpdate || updateDate > latestUpdate) {
latestUpdate = updateDate;
}
});
totalCapacityElement.textContent = `Total capacity: ${totalSpaces}`;
availableCapacityElement.textContent = `Available capacity: ${totalAvailable}`;
if (latestUpdate) {
lastUpdatedElement.textContent = `Last updated: ${latestUpdate.toLocaleString('nl-BE', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
})}`;
}
} else {
totalCapacityElement.textContent = 'No data';
availableCapacityElement.textContent = 'No data';
lastUpdatedElement.textContent = 'No data';
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation for P+R parking:', error);
totalCapacityElement.textContent = 'Error loading data.';
availableCapacityElement.textContent = 'Error loading data.';
lastUpdatedElement.textContent = 'Error loading data.';
});