-
Notifications
You must be signed in to change notification settings - Fork 0
/
student-dashboard.js
57 lines (51 loc) · 1.97 KB
/
student-dashboard.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
let map;
let markers = {};
var shuttleIcon = L.icon({
iconUrl: '/assets/shuttle-icon.png',
iconSize: [38, 38],
iconAnchor: [16, 16],
popupAnchor: [0, -16]
});
function fetchDriverLocations() {
console.log('Fetching driver locations...');
fetch('/updated-locations')
.then(response => response.json())
.then(data => {
if (data.success && data.data) {
console.log('Received locations:', data.data);
data.data.forEach(location => {
if (location && location.latitude && location.longitude) {
updateMap(location.driverId, location.latitude, location.longitude);
} else {
console.error(`Invalid or no location data available for driver ID: ${location.driverId}`);
}
});
} else {
console.log("No locations found or error in response.");
}
})
.catch(error => {
console.error('Failed to fetch driver locations:', error);
});
}
function updateMap(driverId, latitude, longitude) {
console.log(`Updating map for driver: ${driverId}, Lat: ${latitude}, Lng: ${longitude}`);
if (markers[driverId]) {
markers[driverId].setLatLng([latitude, longitude]);
console.log(`Moved marker for driver: ${driverId}`);
} else {
markers[driverId] = L.marker([latitude, longitude], { icon: shuttleIcon, title: driverId }).addTo(map);
console.log(`Created marker for driver: ${driverId}`);
}
}
function initMap() {
map = L.map('map').setView([12.973977, 79.163368], 16);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
fetchDriverLocations();
setInterval(fetchDriverLocations, 5000); // Fetch location updates every 5 seconds
}
document.addEventListener('DOMContentLoaded', () => {
initMap();
});