Files
FireworksApp/frontend/src/scripts/event-map.js
T
2026-10-10 16:03:14 +02:00

110 lines
6.3 KiB
JavaScript

const t = window.BOOMCAST_I18N || {};
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", (t.mapCompass || "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 hasManual = cell.manual_reports > 0;
const hasSensor = cell.sensor_reports > 0;
const color = hasManual && hasSensor ? "#a78bfa" : hasSensor ? "#22d3ee" : "#f59e0b";
const polygon = L.polygon(cell.boundary, {
color, weight: 2, fillColor: color, fillOpacity: 0.25,
}).addTo(map);
polygon.bindPopup(
"<span class=\"popup-title\">H3 " + cell.cell + "</span>" +
"<span class=\"popup-meta\">" + cell.reports + " " + (cell.reports === 1 ? (t.report || "report") : (t.reports || "reports")) + " · " + cell.occurrences +
" " + (cell.occurrences === 1 ? (t.occurrence || "occurrence") : (t.occurrences || "occurrences")) + " · " + (t.resolution || "resolution") + " " + h3Resolution +
"<br>" + cell.manual_reports + " " + (t.manualReport || "Manual report") + " · " + cell.sensor_reports + " " + (t.sensorEvent || "Sensor event") + "</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],
});
const sensorIcon = L.divIcon({
className: "", html: "<div class=\"sensor-icon\">S</div>", iconSize: [37, 37], iconAnchor: [19, 19],
});
reports.forEach((report) => {
const isSensor = report.source_type === "sensor";
const visualStatus = report.visually_confirmed ? (t.visuallyConfirmed || "Visually confirmed") : (t.notVisuallyConfirmed || "Not visually confirmed");
let detail = report.occurrences + " " + (report.occurrences === 1 ? (t.occurrence || "occurrence") : (t.occurrences || "occurrences")) +
" · " + visualStatus + "<br>" + (t.reported || "Reported") + " " + report.reported_on;
if (report.direction_degrees !== null) {
detail += "<br>" + (t.compassDirection || "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: isSensor ? sensorIcon : reportIcon }).addTo(map).bindPopup(
"<span class=\"popup-title\">" + (isSensor ? (t.sensorEvent || "Sensor event") : (t.manualReport || "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");
if (range) {
document.querySelectorAll("#from-date, #to-date").forEach((input) => {
input.addEventListener("change", () => { range.value = "custom"; });
});
}