-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
141 lines (119 loc) · 5.76 KB
/
App.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
document.addEventListener('DOMContentLoaded', () => {
const map = L.map('map').setView([18.5204, 73.8567], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
const markers = {};
let peakCrowd = 0;
let totalCrowd = 0;
let numberOfShops = 0;
function createIcon(color) {
return L.divIcon({
className: 'custom-icon',
html: `<div style="background-color: ${color}; width: 20px; height: 20px; border-radius: 50%;"></div>`,
iconSize: [20, 20]
});
}
// Fetch and update the map with current data
function updateMap() {
fetch('/data')
.then(response => response.json())
.then(data => {
let maxCrowd = 0;
let minCrowd = Infinity;
let minCrowdLocation = '';
totalCrowd = 0;
numberOfShops = 0;
data.forEach(item => {
const key = `${item.coordinates.latitude},${item.coordinates.longitude}`;
const color = item.count > 10 ? 'red' : 'yellow';
if (!markers[key]) {
const marker = L.marker([item.coordinates.latitude, item.coordinates.longitude], { icon: createIcon(color) }).addTo(map)
.bindPopup(`Shop Count: ${item.count}`);
markers[key] = marker;
} else {
markers[key].setPopupContent(`Shop Count: ${item.count}`);
markers[key].setIcon(createIcon(color));
}
// Calculate statistics
if (item.count > maxCrowd) {
maxCrowd = item.count;
}
if (item.count < minCrowd) {
minCrowd = item.count;
minCrowdLocation = `${item.coordinates.latitude},${item.coordinates.longitude}`;
}
totalCrowd += item.count;
numberOfShops++;
});
// Update statistics
const averageCrowd = numberOfShops > 0 ? (totalCrowd / numberOfShops).toFixed(2) : 0;
peakCrowd = maxCrowd;
document.getElementById('peak-crowd').textContent = peakCrowd;
document.getElementById('average-crowd').textContent = averageCrowd;
document.getElementById('preferred-shop').textContent = minCrowdLocation ?
`Shop Location: ${minCrowdLocation} with crowd: ${minCrowd}` : 'No data available';
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
// Function to fetch and display historical data for a selected location
function fetchHistoricalData(lat, lng, minutes) {
fetch(`/history?lat=${lat}&lng=${lng}&minutes=${minutes}`)
.then(response => response.json())
.then(data => {
const historyContainer = document.getElementById('history');
historyContainer.innerHTML = '';
data.forEach(item => {
const historyItem = document.createElement('div');
historyItem.textContent = `Time: ${item.timestamp}, Count: ${item.count}`;
historyContainer.appendChild(historyItem);
});
})
.catch(error => {
console.error('Error fetching historical data:', error);
});
}
updateMap();
setInterval(updateMap, 5000);
// Event listener for viewing historical data
document.getElementById('view-history').addEventListener('click', () => {
const lat = document.getElementById('lat').value;
const lng = document.getElementById('lng').value;
const minutes = document.getElementById('time-range').value;
if (lat && lng && minutes) {
fetchHistoricalData(lat, lng, minutes);
}
});
document.addEventListener('DOMContentLoaded', () => {
const map = L.map('map').setView([18.5204, 73.8567], 13);
// Adding OpenStreetMap tiles (background)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Step 1: Initialize heatmap layer (but leave it empty for now)
let heat = L.heatLayer([], { radius: 25 }).addTo(map);
// Step 2: Fetch data from server and update heatmap
function updateHeatmap() {
fetch('/data') // Assuming your crowd data is at /data
.then(response => response.json()) // Convert the response to JSON
.then(data => {
// Step 3: Prepare the heatmap data in the format [lat, lng, intensity]
const heatmapData = data.map(item => [
item.coordinates.latitude,
item.coordinates.longitude,
item.count // Use crowd count as intensity
]);
// Step 4: Update the heatmap layer with new data
heat.setLatLngs(heatmapData);
})
.catch(error => {
console.error('Error fetching heatmap data:', error);
});
}
// Step 5: Update the heatmap every 5 seconds to reflect changes in crowd data
updateHeatmap(); // Run it once to load data initially
setInterval(updateHeatmap, 5000); // Run it every 5 seconds
});
});