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 = "NESW↑"; L.DomEvent.disableClickPropagation(compass); return compass; }; compassControl.addTo(map); L.tileLayer("https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png", { maxZoom: 18, attribution: "© 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( "H3 " + cell.cell + "" + "" + cell.reports + " reports · " + cell.occurrences + " occurrences · resolution " + h3Resolution + "" ); 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: "
✦
", 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 + "
Reported " + report.reported_on; if (report.direction_degrees !== null) { detail += "
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( "Manual report · H3 " + report.cell + "" + "" + detail + "" ); 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: "
↑
", 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"; }); });