|
| 1 | +<!-- Load Leaflet CSS and JS from CDN --> |
| 2 | +<link |
| 3 | + rel="stylesheet" |
| 4 | + href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.css" |
| 5 | +/> |
| 6 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.js"></script> |
| 7 | + |
| 8 | +<style> |
| 9 | + #map-container { |
| 10 | + width: 100%; |
| 11 | + max-width: 800px; |
| 12 | + margin: 0 auto; |
| 13 | + padding: 20px; |
| 14 | + } |
| 15 | + |
| 16 | + #map { |
| 17 | + height: 500px; |
| 18 | + width: 100%; |
| 19 | + border: 2px solid #ddd; |
| 20 | + border-radius: 4px; |
| 21 | + } |
| 22 | + |
| 23 | + h1 { |
| 24 | + color: #333; |
| 25 | + text-align: center; |
| 26 | + } |
| 27 | + |
| 28 | + .instructions { |
| 29 | + background-color: #f9f9f9; |
| 30 | + padding: 15px; |
| 31 | + border-radius: 4px; |
| 32 | + margin-top: 20px; |
| 33 | + } |
| 34 | +</style> |
| 35 | + |
| 36 | +<div id="map-container"> |
| 37 | + <h1>Interactive Map</h1> |
| 38 | + <div id="map"></div> |
| 39 | + <div class="instructions"> |
| 40 | + <p> |
| 41 | + <strong>Instructions:</strong> You can drag to move the map, scroll to |
| 42 | + zoom in/out, and click on the marker to see information. |
| 43 | + </p> |
| 44 | + </div> |
| 45 | +</div> |
| 46 | + |
| 47 | +<script> |
| 48 | + // Global variables |
| 49 | + let map; |
| 50 | + let geojsonLayer = null; |
| 51 | + const countryMap = new Map(); |
| 52 | + var country_count = {{count_by_country | tojson}} |
| 53 | + |
| 54 | + // Initialize map once |
| 55 | + function initMap() { |
| 56 | + map = L.map( |
| 57 | + "map", { |
| 58 | + maxZoom: 5, |
| 59 | + minZoom: 1 |
| 60 | + } |
| 61 | + ).setView([0, 0], 2); |
| 62 | + L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo( |
| 63 | + map, |
| 64 | + ); |
| 65 | + |
| 66 | + |
| 67 | + // Load GeoJSON once |
| 68 | + fetch("/static/countries.geojson") |
| 69 | + .then((response) => response.json()) |
| 70 | + .then((data) => { |
| 71 | + geojsonLayer = L.geoJSON(data).addTo(map); |
| 72 | + // Only update after geojsonLayer is defined |
| 73 | + updateMapData(); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + // Update map data - check if geojsonLayer exists first |
| 78 | + function updateMapData() { |
| 79 | + if (!geojsonLayer) return; // Skip if geojsonLayer not loaded yet |
| 80 | + |
| 81 | + console.log(country_count) |
| 82 | + geojsonLayer.eachLayer((layer) => { |
| 83 | + const countryName = layer.feature.properties.name; |
| 84 | + const count = country_count[countryName] || 0; |
| 85 | + |
| 86 | + // Update style |
| 87 | + layer.setStyle({ |
| 88 | + color: "#3388ff", |
| 89 | + weight: 1, |
| 90 | + fillColor: "#ff0000", |
| 91 | + fillOpacity: count > 0 ? Math.min(0.20 + count / 200, 0.9) : 0, |
| 92 | + }); |
| 93 | + |
| 94 | + // Update popup |
| 95 | + layer.bindPopup(`${countryName}: ${count}`); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + initMap(); |
| 100 | +</script> |
0 commit comments