Skip to content

Instantly share code, notes, and snippets.

@tristen tristen/app.js
Last active Sep 4, 2019

Embed
What would you like to do?
Zoom Slider
var zoomSlider = document.getElementById('zoom');
var zoomValue = document.getElementById('zoomvalue');
var map, targetZoom;
var initMap = function() {
var template = 'http://{S}tile.openstreetmap.org/{Z}/{X}/{Y}.png';
var subdomains = ['', 'a.', 'b.', 'c.'];
var provider = new MM.TemplatedLayer(template, subdomains);
map = new MM.Map('map', provider, null);
map.setCenterZoom(new MM.Location(37.811530, -122.2666097), 9);
targetZoom = map.getZoom();
zoomSlider.onchange = function() {
var sliderProp = (zoomSlider.value - zoomSlider.min) / (zoomSlider.max - zoomSlider.min);
targetZoom = sliderProp * 18.0;
MM.getFrame(animateToZoom);
};
zoomValue.innerHTML = targetZoom;
map.addCallback('zoomed', function(){
zoomValue.innerHTML = map.getZoom().toFixed(2);
zoomSlider.value = map.getZoom() * 1000;
});
}
var animateToZoom = function() {
var currentZoom = map.getZoom();
var nextZoom = currentZoom + (targetZoom - currentZoom) * 0.2;
if (Math.abs(nextZoom - currentZoom) < 0.001) {
nextZoom = currentZoom;
} else {
MM.getFrame(animateToZoom);
}
map.setZoom(nextZoom);
zoomValue.innerHTML = nextZoom.toFixed(2);
}
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Modest Maps JS Demo | Slide Zoom</title>
<link rel='stylesheet' href='style.css'>
</head>
<body onload='initMap()'>
<div id='map'></div>
<form id='zoom-control'>
<label for='zoom'>Zoom in: </label><span id='zoomvalue'></span>
<input id='zoom' type='range' value='10000' min='0' max='18000'></input>
</form>
<script src='https://rawgithub.com/stamen/modestmaps-js/master/modestmaps.min.js'></script>
<script src='app.js'></script>
</body>
</html>
body {
font: 13px/22px 'Helvetica Neue', Helvetica, sans;
margin: 0;
padding: 0;
}
label {
font-weight: bold;
font-size: 11px;
}
input {
display: block;
}
#map {
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
}
#zoom-control {
background: #fff;
border: 1px solid #808080;
-moz-border-radius: 4px;
border-radius: 4px;
padding: 10px;
position: absolute;
left: 20px; top: 20px;
z-index: 99999999999;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.