Files
mapbattle/map.html

79 lines
2.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Map</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<style>
#map { height: 90vh; }
.legend {
list-style: none;
padding: 0;
width: 200px;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 8px;
}
.color-box {
width: 20px;
height: 20px;
margin-right: 10px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div id="map"></div>
<ul id="legend" class="legend"></ul>
<script>
// <map>
function style(feature) {
return { color: feature.properties.color};
}
var map = L.map('map').setView([47.6062, -122.3321], 12);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
}).addTo(map);
fetch('/geojson')
.then(res => res.json())
.then(data => L.geoJSON(data, {style: style}).addTo(map));
// </map>
// <legend>
const endpoint = "/legend-data";
fetch(endpoint)
.then(response => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then(data => {
const legend = document.getElementById("legend");
// Clear existing content if any
legend.innerHTML = "";
data.forEach(item => {
const li = document.createElement("li");
li.className = "legend-item";
const colorBox = document.createElement("span");
colorBox.className = "color-box";
colorBox.style.backgroundColor = item.color;
const label = document.createElement("span");
label.textContent = item.name;
li.appendChild(colorBox);
li.appendChild(label);
legend.appendChild(li);
});
})
.catch(error => {
console.error("Fetching legend data failed:", error);
});
</script>
</body>
</html>