Skip to content

Create_map.html #697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions _layouts/_includes/map.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!-- Load Leaflet CSS and JS from CDN -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.css"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.js"></script>

<style>
#map-container {
width: 100%;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}

#map {
height: 500px;
width: 100%;
border: 2px solid #ddd;
border-radius: 4px;
}

h1 {
color: #333;
text-align: center;
}

.instructions {
background-color: #f9f9f9;
padding: 15px;
border-radius: 4px;
margin-top: 20px;
}
</style>

<div id="map-container">
<h1>Interactive Map</h1>
<div id="map"></div>
<div class="instructions">
<p>
<strong>Instructions:</strong> You can drag to move the map, scroll to
zoom in/out, and click on the marker to see information.
</p>
</div>
</div>

<script>
// Global variables
let map;
let geojsonLayer = null;
const countryMap = new Map();
var country_count = {{count_by_country | tojson}}

// Initialize map once
function initMap() {
map = L.map(
"map", {
maxZoom: 5,
minZoom: 1
}
).setView([0, 0], 2);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(
map,
);


// Load GeoJSON once
fetch("/static/countries.geojson")
.then((response) => response.json())
.then((data) => {
geojsonLayer = L.geoJSON(data).addTo(map);
// Only update after geojsonLayer is defined
updateMapData();
});
}

// Update map data - check if geojsonLayer exists first
function updateMapData() {
if (!geojsonLayer) return; // Skip if geojsonLayer not loaded yet

console.log(country_count)
geojsonLayer.eachLayer((layer) => {
const countryName = layer.feature.properties.name;
const count = country_count[countryName] || 0;

// Update style
layer.setStyle({
color: "#3388ff",
weight: 1,
fillColor: "#ff0000",
fillOpacity: count > 0 ? Math.min(0.20 + count / 200, 0.9) : 0,
});

// Update popup
layer.bindPopup(`${countryName}: ${count}`);
});
}

initMap();
</script>