Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active April 12, 2016 15:01
Show Gist options
  • Save ramiroaznar/b9e589cd0942eaad5139 to your computer and use it in GitHub Desktop.
Save ramiroaznar/b9e589cd0942eaad5139 to your computer and use it in GitHub Desktop.
Reset Zoom Buttom with CartoDB.js

Drag the map to other location or zoom in or out with your mouse scrollroll. Then click on the "Reset zoom" bottom to come back to the original view.

<!DOCTYPE html>
<html>
<head>
<title>Reset Zoom Buttom with CartoDB.js</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
#reset{
position: absolute;
top: 20px;
left: 20px;
padding-right: 10px;
padding-left: 10px;
z-index: 9000;
background-color: grey;
cursor:pointer;
}
</style>
</head>
<body>
<div id="map"></div>
<div id = 'reset'><p>Reset Zoom</p></div>
<script type="text/javascript">
var layer;
var vector = [];
var legend;
function main() {
var map = L.map('map', {
zoomControl: false,
center: [41.390205, 2.154007],
zoom: 12,
minZoom:9,
maxZoom:13
});
// zoom in function
$('#reset').click(function(){
map.setView([41.390205, 2.154007],12);
});
// add basemap
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'}).addTo(map);
// add CartoDB layer
cartodb.createLayer(map, {
user_name: 'oboix',
type: 'cartodb',
sublayers: [{
sql: "select * FROM barris_barcelona_test;",
cartocss: "#barris_barcelona_test{ polygon-fill: #29ABA4;polygon-opacity: 1;line-color: #FFF;line-width: 0.5;line-opacity: 1; }",
}]
}).addTo(map)
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment