Skip to content

Instantly share code, notes, and snippets.

@hhkaos
Created August 13, 2024 13:11
Show Gist options
  • Save hhkaos/3852631d45ef226f2c195b400c740b73 to your computer and use it in GitHub Desktop.
Save hhkaos/3852631d45ef226f2c195b400c740b73 to your computer and use it in GitHub Desktop.
Leaflet Legend
<div id="mapid"></div>
<!--http://leafletjs.com/examples/choropleth/-->
/*Setup*/
var map = L.map("mapid").setView([55.67, 12.57], 7);
L.tileLayer(
"https://stamen-tiles.a.ssl.fastly.net/terrain/{z}/{x}/{y}.jpg"
).addTo(map);
/*Legend specific*/
var legend = L.control({ position: "bottomleft" });
legend.onAdd = function(map) {
var div = L.DomUtil.create("div", "legend");
div.innerHTML += "<h4>Tegnforklaring</h4>";
div.innerHTML += '<i style="background: #477AC2"></i><span>Water</span><br>';
div.innerHTML += '<i style="background: #448D40"></i><span>Forest</span><br>';
div.innerHTML += '<i style="background: #E6E696"></i><span>Land</span><br>';
div.innerHTML += '<i style="background: #E8E6E0"></i><span>Residential</span><br>';
div.innerHTML += '<i style="background: #FFFFFF"></i><span>Ice</span><br>';
div.innerHTML += '<i class="icon" style="background-image: url(https://d30y9cdsu7xlg0.cloudfront.net/png/194515-200.png);background-repeat: no-repeat;"></i><span>Grænse</span><br>';
return div;
};
legend.addTo(map);
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://stamen-maps.a.ssl.fastly.net/js/tile.stamen.js"></script>
/*Setup*/
html,
body {
padding: 0;
margin: 0;
}
html,
body,
#mapid {
height: 100%;
width: 100%;
}
/*Legend specific*/
.legend {
padding: 6px 8px;
font: 14px Arial, Helvetica, sans-serif;
background: white;
background: rgba(255, 255, 255, 0.8);
/*box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);*/
/*border-radius: 5px;*/
line-height: 24px;
color: #555;
}
.legend h4 {
text-align: center;
font-size: 16px;
margin: 2px 12px 8px;
color: #777;
}
.legend span {
position: relative;
bottom: 3px;
}
.legend i {
width: 18px;
height: 18px;
float: left;
margin: 0 8px 0 0;
opacity: 0.7;
}
.legend i.icon {
background-size: 18px;
background-color: rgba(255, 255, 255, 1);
}
<link href="https://unpkg.com/[email protected]/dist/leaflet.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment