-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
313 lines (276 loc) · 9.71 KB
/
Copy pathscript.js
File metadata and controls
313 lines (276 loc) · 9.71 KB
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
let map;
let userMarker;
let serviceMarkers = [];
let floodZoneLayers = [];
let currentFilter = "all";
let userLocation = null;
// Flood Zones Data
const floodZones = [
{
name: "Hiranandani Gardens Basin",
risk: "LOW",
coordinates: [
[19.120, 72.910],
[19.125, 72.915],
[19.122, 72.920],
[19.117, 72.915],
],
},
{
name: "IIT Bombay Low-lying Area",
risk: "MEDIUM",
coordinates: [
[19.132, 72.911],
[19.137, 72.917],
[19.134, 72.922],
[19.129, 72.916],
],
},
];
// Utility Functions
function calculateDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Earth's radius in km
const dLat = ((lat2 - lat1) * Math.PI) / 180;
const dLon = ((lon2 - lon1) * Math.PI) / 180;
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos((lat1 * Math.PI) / 180) *
Math.cos((lat2 * Math.PI) / 180) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return (R * c).toFixed(1); // Distance in km
}
function getServiceType(tags) {
if (tags.amenity === "hospital" || tags.building === "hospital") return "medical";
if (tags.amenity === "fire_station") return "rescue";
if (tags.amenity === "police") return "rescue";
if (tags.amenity === "shelter") return "shelter";
return "other";
}
function getMarkerColor(type) {
switch (type) {
case "medical": return "#2ecc71";
case "rescue": return "#f1c40f";
case "shelter": return "#3498db";
default: return "#e74c3c";
}
}
function createServiceMarker(service) {
const type = getServiceType(service.tags);
const markerColor = getMarkerColor(type);
if (!service.tags.name) return null; // Exclude unknown services
const address = service.tags["addr:full"] ||
(service.tags["addr:district"] + ", " + service.tags["addr:state"] + ", " + service.tags["addr:postcode"]);
const marker = L.marker([service.lat, service.lon], {
icon: L.divIcon({
className: "service-marker",
html: `<div style="background-color: ${markerColor}; width: 24px; height: 24px; border-radius: 50%; border: 3px solid white; box-shadow: 0 2px 4px rgba(0,0,0,0.2);"></div>`,
iconSize: [24, 24],
}),
});
const popupContent = `
<div class="popup-content">
<div class="popup-title">${service.tags.name}</div>
<div class="popup-details">${address || "No address available"}</div>
<div class="popup-details">Distance: ${calculateDistance(userLocation.lat, userLocation.lng, service.lat, service.lon)} km</div>
<div class="popup-details">Flood Risk: LOW</div>
</div>
`;
marker.bindPopup(popupContent);
return { marker, type, details: popupContent, source: service.tags.source };
}
// Sort Services by Source (OpenGovernmentData first)
function sortServicesBySource(services) {
return services.sort((a, b) => {
if (a.source === "OpenGovernmentData" && b.source !== "OpenGovernmentData") {
return -1;
}
if (b.source === "OpenGovernmentData" && a.source !== "OpenGovernmentData") {
return 1;
}
return 0; // No change if both or neither are OpenGovernmentData
});
}
async function fetchNearbyServices(lat, lon, radius) {
const query = `
[out:json][timeout:25];
(
node["amenity"="hospital"](around:${radius},${lat},${lon});
node["amenity"="fire_station"](around:${radius},${lat},${lon});
node["amenity"="police"](around:${radius},${lat},${lon});
node["amenity"="shelter"](around:${radius},${lat},${lon});
node["building"="hospital"](around:${radius},${lat},${lon});
);
out body;
>;
out skel qt;
`;
const response = await fetch("https://overpass-api.de/api/interpreter", {
method: "POST",
body: query,
});
const data = await response.json();
return data.elements;
}
// Flood Zones Handling
function addFloodZones() {
floodZoneLayers.forEach(layer => map.removeLayer(layer)); // Remove existing flood zone layers
floodZoneLayers = []; // Reset layers array
floodZones.forEach(zone => {
const polygon = L.polygon(zone.coordinates, {
color: zone.risk === "HIGH" ? "red" : zone.risk === "MEDIUM" ? "orange" : "yellow",
fillColor: zone.risk === "HIGH" ? "red" : zone.risk === "MEDIUM" ? "orange" : "yellow",
fillOpacity: 0.3,
weight: 2,
});
polygon.bindPopup(`
<strong>${zone.name}</strong><br>
Flood Risk Level: ${zone.risk}<br>
<small>Stay alert during heavy rainfall</small>
`);
polygon.addTo(map);
floodZoneLayers.push(polygon); // Add to array of layers
});
}
function toggleFloodZones(show) {
if (show) {
addFloodZones(); // Show flood zones
} else {
floodZoneLayers.forEach(layer => map.removeLayer(layer)); // Hide flood zones
}
}
// Populate Services List
function populateServicesList() {
const container = document.getElementById("services-list");
container.innerHTML = ""; // Clear the list
// Sort services so "OpenGovernmentData" sources come first
const sortedServices = sortServicesBySource(serviceMarkers);
sortedServices.forEach(({ marker, type, details }) => {
if (currentFilter === "all" || type === currentFilter) {
const card = document.createElement("div");
card.className = "service-card";
card.innerHTML = `<div class="service-info">${details}</div>`;
card.addEventListener("click", () => {
map.setView(marker.getLatLng(), 16);
marker.openPopup();
});
container.appendChild(card);
}
});
if (currentFilter === "floodzones") {
floodZones.forEach(zone => {
const card = document.createElement("div");
card.className = "service-card";
card.innerHTML = `
<div class="service-info">
<strong>${zone.name}</strong><br>
Flood Risk: ${zone.risk}<br>
Distance: ${calculateDistance(userLocation.lat, userLocation.lng, zone.coordinates[0][0], zone.coordinates[0][1])} km
</div>`;
card.addEventListener("click", () => {
map.fitBounds(zone.coordinates); // Zoom to the flood zone
});
container.appendChild(card);
});
}
}
// Filter Markers by Type
function filterMarkers(filterType) {
serviceMarkers.forEach(({ marker, type }) => {
if (filterType === "all" || type === filterType) {
marker.addTo(map);
} else {
map.removeLayer(marker);
}
});
toggleFloodZones(filterType === "floodzones"); // Toggle flood zones visibility
populateServicesList();
}
function addLegend() {
const legend = L.control({ position: "bottomright" });
legend.onAdd = function () {
const div = L.DomUtil.create("div", "legend");
div.innerHTML = `
<h4>Flood Risk Levels</h4>
<div class="legend-item">
<div class="legend-color" style="background: rgba(255, 0, 0, 0.5);"></div>
High Risk
</div>
<div class="legend-item">
<div class="legend-color" style="background: rgba(255, 165, 0, 0.5);"></div>
Medium Risk
</div>
<div class="legend-item">
<div class="legend-color" style="background: rgba(255, 255, 0, 0.5);"></div>
Low Risk
</div>
`;
return div;
};
legend.addTo(map);
}
// Map Initialization
async function initMap() {
map = L.map("map").setView([userLocation.lat, userLocation.lng], 13);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© OpenStreetMap contributors",
}).addTo(map);
userMarker = L.marker([userLocation.lat, userLocation.lng], {
icon: L.divIcon({
className: "user-marker",
html: "📍",
iconSize: [25, 25],
}),
}).addTo(map);
userMarker.bindPopup("Your Location").openPopup();
addLegend();
try {
const services = await fetchNearbyServices(userLocation.lat, userLocation.lng, 5000);
services.forEach((service) => {
const markerData = createServiceMarker(service);
if (markerData) {
const { marker, type, details, source } = markerData;
marker.addTo(map);
serviceMarkers.push({ marker, type, details, source });
}
});
populateServicesList();
} catch (error) {
console.error("Error fetching services:", error);
document.getElementById("locationStatus").textContent = "Error loading nearby services.";
}
}
function getUserLocation() {
const locationStatus = document.getElementById("locationStatus");
if (!navigator.geolocation) {
locationStatus.textContent = "Geolocation is not supported by your browser.";
return;
}
navigator.geolocation.getCurrentPosition(
(position) => {
userLocation = { lat: position.coords.latitude, lng: position.coords.longitude };
locationStatus.textContent = "Location detected! Showing nearby services...";
initMap();
},
() => {
locationStatus.textContent = "Unable to get your location. Showing default area...";
userLocation = { lat: 19.1334, lng: 72.9133 }; // Default to Mumbai
initMap();
}
);
}
document.getElementById("locateMe").addEventListener("click", () => {
map.setView([userLocation.lat, userLocation.lng], 13);
userMarker.openPopup();
});
document.querySelectorAll(".nav-button").forEach((button) => {
button.addEventListener("click", () => {
document.querySelectorAll(".nav-button").forEach((btn) => (btn.style.opacity = "1"));
button.style.opacity = "0.8";
const filterType = button.id.toLowerCase().replace("services", "");
currentFilter = filterType;
filterMarkers(filterType);
});
});
getUserLocation();