Skip to content

Instantly share code, notes, and snippets.

@fawcett
Last active August 29, 2015 14:16
Show Gist options
  • Save fawcett/f42704d7620a9bb98f85 to your computer and use it in GitHub Desktop.
Save fawcett/f42704d7620a9bb98f85 to your computer and use it in GitHub Desktop.
Transparent Legend - Mapbox.js

An example showing how to make a transparent custom legend with mapbox.js. Code is based on an example by Tristen.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Custom legend</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v1.6.3/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v1.6.3/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<style>
.legend label,
.legend span {
display:block;
float:left;
height:15px;
width:20%;
text-align:center;
font-size:9px;
color:#000000;
}
.map-legends.wax-legends {
background: rgba(255,255,255,.5);
}
</style>
<!-- Set the display of this container to none so we can
add it programmatically to `legendControl` -->
<div id='legend' style='display:none;'>
<strong>The Title or Explanation of your Map</strong>
<nav class='legend clearfix'>
<span style='background:#F1EEF6;'></span>
<span style='background:#BDC9E1;'></span>
<span style='background:#74A9CF;'></span>
<span style='background:#2B8CBE;'></span>
<span style='background:#045A8D;'></span>
<label>0 - 20%</label>
<label>40%</label>
<label>60%</label>
<label>80%</label>
<label>100%</label>
<small>Source: <a href="#link to source">Name of source</a></small>
</div>
<div id='map' class='dark'></div>
<script>
var map = L.mapbox.map('map', 'examples.map-y7l23tes');
map.legendControl
.addLegend(document.getElementById('legend').innerHTML);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment