Files
FireworksApp/frontend/src/scripts/event-map.js
T

98 lines
5.2 KiB
JavaScript

const mapElement = document.getElementById("event-map");
const h3Resolution = mapElement.dataset.h3Resolution;
const map = L.map(mapElement, { zoomControl: false, scrollWheelZoom: false }).setView([52.1326, 5.2913], 7);
L.control.zoom({ position: "bottomleft" }).addTo(map);
const compassControl = L.control({ position: "topright" });
compassControl.onAdd = () => {
const compass = L.DomUtil.create("div", "map-compass");
compass.setAttribute("role", "img");
compass.setAttribute("aria-label", "Map compass: north is up");
compass.innerHTML = "<span class=\"compass-n\">N</span><span class=\"compass-e\">E</span><span class=\"compass-s\">S</span><span class=\"compass-w\">W</span><span class=\"compass-needle\">↑</span>";
L.DomEvent.disableClickPropagation(compass);
return compass;
};
compassControl.addTo(map);
L.tileLayer("https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png", {
maxZoom: 18,
attribution: "&copy; OpenStreetMap contributors",
}).addTo(map);
const h3Cells = JSON.parse(document.getElementById("h3-cells-data").textContent);
const reports = JSON.parse(document.getElementById("map-events-data").textContent);
const bounds = L.latLngBounds([]);
h3Cells.forEach((cell) => {
const polygon = L.polygon(cell.boundary, {
color: "#f59e0b", weight: 2, fillColor: "#f59e0b", fillOpacity: 0.25,
}).addTo(map);
polygon.bindPopup(
"<span class=\"popup-title\">H3 " + cell.cell + "</span>" +
"<span class=\"popup-meta\">" + cell.reports + " reports · " + cell.occurrences +
" occurrences · resolution " + h3Resolution + "</span>"
);
bounds.extend(L.latLngBounds(cell.boundary));
});
function cardinalDirection(degrees) {
const directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
return directions[Math.floor(((degrees + 22.5) % 360) / 45)];
}
function destinationPoint(origin, bearingDegrees, distanceMetres) {
const earthRadius = 6378137;
const bearing = bearingDegrees * Math.PI / 180;
const latitude = origin[0] * Math.PI / 180;
const longitude = origin[1] * Math.PI / 180;
const angularDistance = distanceMetres / earthRadius;
const targetLatitude = Math.asin(
Math.sin(latitude) * Math.cos(angularDistance) +
Math.cos(latitude) * Math.sin(angularDistance) * Math.cos(bearing)
);
const targetLongitude = longitude + Math.atan2(
Math.sin(bearing) * Math.sin(angularDistance) * Math.cos(latitude),
Math.cos(angularDistance) - Math.sin(latitude) * Math.sin(targetLatitude)
);
return [targetLatitude * 180 / Math.PI, targetLongitude * 180 / Math.PI];
}
const reportIcon = L.divIcon({
className: "", html: "<div class=\"report-icon\">✦</div>", iconSize: [37, 37], iconAnchor: [19, 19],
});
reports.forEach((report) => {
const visualStatus = report.visually_confirmed ? "Visually confirmed" : "Not visually confirmed";
let detail = report.occurrences + " occurrence" + (report.occurrences === 1 ? "" : "s") +
" · " + visualStatus + "<br>Reported " + report.reported_on;
if (report.direction_degrees !== null) {
detail += "<br>Compass direction " + Math.round(report.direction_degrees) + "° (" + cardinalDirection(report.direction_degrees) + ")";
if (report.direction_accuracy_degrees !== null) {
detail += " (±" + Math.round(report.direction_accuracy_degrees) + "°)";
}
}
L.marker(report.center, { icon: reportIcon }).addTo(map).bindPopup(
"<span class=\"popup-title\">Manual report · H3 " + report.cell + "</span>" +
"<span class=\"popup-meta\">" + detail + "</span>"
);
if (report.direction_degrees !== null) {
const arrowEnd = destinationPoint(report.center, report.direction_degrees, 900);
L.polyline([report.center, arrowEnd], { color: "#fff7e6", weight: 9, opacity: 0.8 }).addTo(map);
L.polyline([report.center, arrowEnd], { color: "#6d45a0", weight: 5, opacity: 1 }).addTo(map);
const arrowIcon = L.divIcon({
className: "",
html: "<div class=\"direction-marker\"><span style=\"transform:rotate(" + report.direction_degrees + "deg)\">↑</span></div>",
iconSize: [56, 56], iconAnchor: [28, 28],
});
L.marker(arrowEnd, { icon: arrowIcon, interactive: false }).addTo(map);
}
});
if (bounds.isValid()) {
map.fitBounds(bounds.pad(0.18));
}
const range = document.getElementById("range");
document.querySelectorAll("#from-date, #to-date").forEach((input) => {
input.addEventListener("change", () => { range.value = "custom"; });
});