Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Last active December 4, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wboykinm/8d331f9b5c7dd57908de0db3439ab7ab to your computer and use it in GitHub Desktop.
Save wboykinm/8d331f9b5c7dd57908de0db3439ab7ab to your computer and use it in GitHub Desktop.
MapboxGL threshold style slider
license: mit
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Adjust a layer</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.45.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.45.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>Score threshold: <span id='slider-value'>0.75</span></label>
<input id='slider' type='range' min='0' max='1' step='0.05' value='0.75' />
</div>
</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZmFyYWRheTIiLCJhIjoiTUVHbDl5OCJ9.buFaqIdaIM3iXr1BOYKpsQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/faraday2/cjidg4nxa1c102spa1p0gy662',
center: [-87.6321, 41.8362],
minZoom: 5,
maxZoom: 13,
zoom: 9.5
});
var slider = document.getElementById('slider');
var sliderValue = document.getElementById('slider-value');
map.addControl(new mapboxgl.NavigationControl());
map.on('load', function() {
map.addLayer({
'id': 'scores',
'type': 'circle',
'source': {
'type': 'geojson',
'data': 'https://rawgit.com/wboykinm/be013c300ee9a6e0cea2897a9de56fb3/raw/72319ef9a549634360cc6b6186fc81357fe8385f/chicago.geojson'
},
'paint': {
'circle-radius': [
'step',
['get', 'value'],
6, 0.7,
16
],
'circle-color': [
'step',
['get', 'value'],
'#fff', 0.7,
'#B26BFB'
]
}
});
slider.addEventListener('input', function(e) {
map.setPaintProperty('scores', 'circle-color', [ 'step', ['get', 'value'], '#fff', parseFloat(e.target.value, 1), '#B26BFB' ]);
map.setPaintProperty('scores', 'circle-radius', [ 'step', ['get', 'value'], 6, parseFloat(e.target.value, 1), 16 ]);
sliderValue.textContent = e.target.value;
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment