Skip to content

Instantly share code, notes, and snippets.

@andyrutkowski
Last active September 1, 2018 17:45
Show Gist options
  • Save andyrutkowski/c3971c105979565acc3e68b318f006d7 to your computer and use it in GitHub Desktop.
Save andyrutkowski/c3971c105979565acc3e68b318f006d7 to your computer and use it in GitHub Desktop.
mapbox layer toggle
license: mit
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Show and hide layers</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<style>
#menu {
background: #fff;
position: absolute;
z-index: 1;
top: 10px;
right: 10px;
border-radius: 3px;
width: 120px;
border: 1px solid rgba(0,0,0,0.4);
font-family: 'Open Sans', sans-serif;
}
#menu a {
font-size: 13px;
color: #404040;
display: block;
margin: 0;
padding: 0;
padding: 10px;
text-decoration: none;
border-bottom: 1px solid rgba(0,0,0,0.25);
text-align: center;
}
#menu a:last-child {
border: none;
}
#menu a:hover {
background-color: #f8f8f8;
color: #404040;
}
#menu a.active {
background-color: #3887be;
color: #ffffff;
}
#menu a.active:hover {
background: #3074a4;
}
</style>
<nav id="menu"></nav>
<div id="map"></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiYW5keS1ydXRrb3dza2kiLCJhIjoiWDJRMFVUWSJ9.iHl6Cf0M79wmHQlfP6Bk8Q';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/andy-rutkowski/cj5ij2u2056372srpho3je4dl',
zoom: 8,
center: [-118.296271, 33.989834]
});
map.on('load', function () {
map.addSource('watts-curfew-map-dupwjz', {
type: 'raster',
url: 'mapbox://andy-rutkowski.0tqu2dvy'
});
map.addLayer({
'id': 'Watts',
'type': 'raster',
'source': 'watts-curfew-map-dupwjz',
'layout': {
'visibility': 'visible'
},
'source-layer': 'watts-curfew-map-dupwjz'
});
map.addSource('CA_LosAngeles1_193911-3w46o8', {
type: 'raster',
url: 'mapbox://andy-rutkowski.cbzfjftf'
});
map.addLayer({
'id': 'HOLC',
'type': 'raster',
'source': 'CA_LosAngeles1_193911-3w46o8',
'source-layer': 'CA_LosAngeles1_193911-3w46o8',
'layout': {
'visibility': 'visible',
},
});
});
var toggleableLayerIds = [ 'Watts', 'HOLC' ];
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
var link = document.createElement('a');
link.href = '#';
link.className = 'active';
link.textContent = id;
link.onclick = function (e) {
var clickedLayer = this.textContent;
e.preventDefault();
e.stopPropagation();
var visibility = map.getLayoutProperty(clickedLayer, 'visibility');
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = '';
} else {
this.className = 'active';
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
}
};
var layers = document.getElementById('menu');
layers.appendChild(link);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment