forked from adiprem73/JeevanDaan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvacscript.js
More file actions
82 lines (73 loc) · 2.72 KB
/
vacscript.js
File metadata and controls
82 lines (73 loc) · 2.72 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
let vaccinationCenters = [];
// Fetch data from the vacdata.json file
fetch("vaccine/vaccine.json")
.then((response) => response.json())
.then((data) => {
vaccinationCenters = data;
})
.catch((error) => {
console.error("Error loading vaccination data:", error);
});
// Convert degrees to radians
const toRadians = (degrees) => degrees * (Math.PI / 180);
// Calculate distance between two coordinates using the Haversine formula
const calculateDistance = (lat1, lon1, lat2, lon2) => {
const R = 6371; // Earth's radius in km
const dLat = toRadians(lat2 - lat1);
const dLon = toRadians(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // Distance in km
};
// Handle location permission and filter centers
document.getElementById("get-location").addEventListener("click", () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const userLat = position.coords.latitude;
const userLon = position.coords.longitude;
const radius = parseInt(document.getElementById("radius").value, 10);
console.log(userLat);
console.log(userLon);
console.log(radius);
const nearbyCenters = vaccinationCenters.filter((center) => {
const distance = calculateDistance(userLat, userLon, center.Latitude, center.Longitude);
return distance <= radius;
});
displayResults(nearbyCenters);
},
(error) => {
alert("Unable to retrieve your location. Please allow location access.");
}
);
} else {
alert("Geolocation is not supported by this browser.");
}
});
// Display results dynamically
const displayResults = (centers) => {
const resultsDiv = document.getElementById("results");
resultsDiv.innerHTML = ""; // Clear previous results
if (centers.length === 0) {
resultsDiv.innerHTML = "<p>No vaccination centers found within the selected radius.</p>";
} else {
centers.forEach((center) => {
const card = document.createElement("div");
card.className = "result-card";
card.innerHTML = `
<h2>${center["Disease Name"]}</h2>
<p><strong>Address:</strong> ${center.Address}</p>
<p><strong>Date:</strong> ${center.Date}</p>
<p><strong>Age Group:</strong> ${center["Age Group"]}</p>
`;
resultsDiv.appendChild(card);
});
}
};
// Update radius display dynamically
document.getElementById("radius").addEventListener("input", (event) => {
document.getElementById("radius-value").innerText = `${event.target.value} km`;
});