-
Notifications
You must be signed in to change notification settings - Fork 0
Mapbox sample code for historical maps
Yoh Kawano edited this page Aug 10, 2017
·
4 revisions
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- Access jQuery Library -->
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/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;
}
#la1939-toggle {
position: absolute;
top: 10px;
right: 10px;
width: 150px;
height: 30px;
border: 1px solid black;
background-color: white;
}
#la1940-toggle {
position: absolute;
top: 40px;
right: 10px;
width: 150px;
height: 30px;
border: 1px solid black;
background-color: white;
}
</style>
<div id='map'></div>
<div id='la1939-toggle'>toggle</div>
<div id='la1940-toggle'>toggle</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>
mapboxgl.accessToken = 'pk.eyJ1IjoieW9obWFuIiwiYSI6IkxuRThfNFkifQ.u2xRJMiChx914U7mOZMiZw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: [-118, 34],
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": "la1940",
"source": {
"type": "raster",
"url": "mapbox://yohman.4cr0620h"
},
"type": "raster"
});
map.addLayer({
"id": "la1939",
"source": {
"type": "raster",
"url": "mapbox://andy-rutkowski.cbzfjftf"
},
"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('la1940', 'raster-opacity', parseInt(e.target.value, 10) / 100);
// Value indicator
sliderValue.textContent = e.target.value + '%';
});
$('#la1939-toggle').click(function(){
if(map.getPaintProperty('la1939','raster-opacity') == 0)
{
map.setPaintProperty('la1939', 'raster-opacity', 1)
}
else
{
map.setPaintProperty('la1939', 'raster-opacity', 0)
}
})
$('#la1940-toggle').click(function(){
if(map.getPaintProperty('la1940','raster-opacity') == 0)
{
map.setPaintProperty('la1940', 'raster-opacity', 1)
}
else
{
map.setPaintProperty('la1940', 'raster-opacity', 0)
}
})
});
</script>
</body>
</html>