Built with blockbuilder.org
xxxxxxxxxx
//Lines 4 - 51 Set up the map and the style.
//Lines 53 - 60 Insert the map and the Layer Opacity info box.
//Lines 63 -73 Pull in your specific map data from MapBox. Line 67 is your you would insert the style from your Mapbox account.
<html>
<head>
<meta charset='utf-8' />
<title>Adjust a layer's opacity</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.49.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.49.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>
.map-overlay {
font:bold 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
position: absolute;
width: 25%;
top: 0;
left: 0;
padding: 10px;
}
.map-overlay .map-overlay-inner {
background-color: #fff;
box-shadow:0 1px 2px rgba(0, 0, 0, 0.20);
border-radius: 3px;
padding: 10px;
margin-bottom: 10px;
}
.map-overlay label {
display: block;
margin: 0 0 10px;
}
.map-overlay input {
background-color: transparent;
display: inline-block;
width: 100%;
position: relative;
margin: 0;
cursor: ew-resize;
}
</style>
<div id='map'></div>
<div class='map-overlay top'>
<div class='map-overlay-inner'>
<label>Layer opacity: <span id='slider-value'>100%</span></label>
<input id='slider' type='range' min='0' max='100' step='0' value='100' />
</div>
</div>
<script>
//this is the default access token. You can customize with your own from your Mapbox account.
mapboxgl.accessToken = 'pk.eyJ1IjoiYW5keS1ydXRrb3dza2kiLCJhIjoiWDJRMFVUWSJ9.iHl6Cf0M79wmHQlfP6Bk8Q';
var map = new mapboxgl.Map({
container: 'map',
//Insert your Mapbox style in the next line.
style: 'mapbox://styles/andy-rutkowski/cjmyg19qb2t712rmq2pvldq00',
center: [-118.268790, 33.968554],
minZoom: 9.5,
maxZoom: 13,
zoom: 9.5
});
var slider = document.getElementById('slider');
var sliderValue = document.getElementById('slider-value');
map.on('load', function() {
map.addLayer({
"id": "HOLC1939",
"source": {
"type": "raster",
"url": "mapbox://andy-rutkowski.a0p1p0hr"
},
"type": "raster"
});
slider.addEventListener('input', function(e) {
// Adjust the layers opacity. layer here is arbitrary - this could
// be another layer name found in your style or a custom layer
// added on the fly using `addSource`.
map.setPaintProperty('HOLC1939', 'raster-opacity', parseInt(e.target.value, 10) / 100);
// Value indicator
sliderValue.textContent = e.target.value + '%';
});
});
</script>
</body>
</html>
https://api.tiles.mapbox.com/mapbox-gl-js/v0.49.0/mapbox-gl.js