Created
November 30, 2017 14:19
-
-
Save Pabloska/a860115cc78515291688ca4491f3ba09 to your computer and use it in GitHub Desktop.
Carto clusters + sql filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="https://www.google.com/jsapi"></script> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> | |
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" /> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script> | |
<!--BOOTSTRAP SET--> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> | |
<style> | |
html, body, #map { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
font-family: "Lato",sans-serif; | |
} | |
ul.share-buttons{ | |
list-style: none; | |
padding: 0; | |
} | |
.cartodb-legend { | |
width: 220px; | |
height: auto; | |
position: absolute; | |
bottom: 20px !important; | |
background-color:rgba(0, 0, 0, 0.5); | |
right: 20px; | |
} | |
@media screen and (max-width: 501px) { | |
.cartodb-legend { display: none; } /* hide it elsewhere */} | |
<!-- Attribution--> | |
.leaflet-container .leaflet-control-attribution{ | |
color: red; -> change color of text | |
background-color: transparent; | |
} | |
.leaflet-container .leaflet-control-attribution a{ | |
color: grey; -> change the color of the hyperlinks | |
opacity:0; | |
} | |
div.cartodb-timeslider{ display: none!important; } | |
<!-- DROPDOWN MENU--> | |
.btn-group-vertical>.btn, .btn-group>.btn{ | |
padding: 0px; | |
min-width: 170px; | |
} | |
.dropdown-menu{ | |
padding: 0px; | |
margin:0px; | |
max-width: 170px; | |
font-size:13px; | |
} | |
.row { | |
// min-width: 200px; | |
text-align:center; | |
margin-top:20px | |
} | |
</style> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" /> | |
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> | |
</head> | |
<body> | |
<div id="map"></div> | |
<!--FILTERs--> | |
<div style="position:absolute;top: 50px;right: 50%;left: 50%;z-index: 900;margin-left:-85px"> | |
<!--genders--> | |
<div class="btn-group"> | |
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> | |
<span class="selection">Population </span><span class="caret"></span> | |
</button> | |
<ul class="dropdown-menu" role="menu"> | |
<li><a href="#a" id="a" class="button">< 250000</a></li> | |
<li><a href="#b" id="b" class="button">> 250000</a></li> | |
<li><a href="#c" id="c" class="button">< 5000 </a></li> | |
</ul> | |
</div> | |
</div> | |
<script> | |
function main(){ | |
vizJson='https://team.carto.com/u/ramirocartodb/api/v2/viz/1bbfec12-ef16-4897-8566-8491193b5e56/viz.json' | |
cartodb.createVis('map', vizJson, {zoom:3}) | |
.on('done', function(vis,layers) { | |
// get layer | |
let subLayer = layers[1].getSubLayer(0), | |
// get SQL cluster query | |
query = subLayer._parent.layers[0].options.sql; | |
console.log(query); | |
console.log(subLayer); | |
// add button action | |
$('#a').on('click', function() { | |
subLayer.setSQL(query.replace('SELECT * FROM ne_10m_populated_places_simple', 'SELECT * FROM ne_10m_populated_places_simple where pop_max < 250000')); | |
}); | |
$('#b').on('click', function() { | |
subLayer.setSQL(query.replace('SELECT * FROM ne_10m_populated_places_simple', 'SELECT * FROM ne_10m_populated_places_simple where pop_max > 250000')); | |
}); | |
$('#c').on('click', function() { | |
subLayer.setSQL(query.replace('SELECT * FROM ne_10m_populated_places_simple', 'SELECT * FROM ne_10m_populated_places_simple where pop_max < 5000')); | |
}); | |
}).on('error', function() { | |
console.log("some error occurred"); | |
}); | |
} | |
// you could use $(window).load(main); | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment