Skip to content

Instantly share code, notes, and snippets.

@iriberri
Created September 3, 2015 12:57
Show Gist options
  • Save iriberri/a9b46bf7597cc177caec to your computer and use it in GitHub Desktop.
Save iriberri/a9b46bf7597cc177caec to your computer and use it in GitHub Desktop.
CartoCSS from SQL-retrieved data
<!DOCTYPE html>
<html>
<head>
<title>Change marker example | 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="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<script>
function main() {
var my_layer;
// create leaflet map
var map = L.map('map', {
zoomControl: false,
center: [43, 0],
zoom: 3
})
// add a base layer
L.tileLayer('http://tile.stamen.com/toner/{z}/{x}/{y}.png', {
attribution: 'Map tiles by Stament. CartoDB attribution.'
}).addTo(map);
// add cartodb layer with one sublayer
cartodb.createLayer(map, {
user_name: 'iriberri',
type: 'cartodb',
sublayers: [{
sql: 'select * from world_borders_1',
cartocss: '#world_borders_1{ polygon-fill: #FFFFB2; polygon-opacity: 0.8; line-color: #FFF; line-width: 0.5; line-opacity: 1; } #world_borders_1 [ pop2005 <= 1312978855] { polygon-fill: #B10026; } #world_borders_1 [ pop2005 <= 32270507] { polygon-fill: #E31A1C; } #world_borders_1 [ pop2005 <= 10528226] { polygon-fill: #FC4E2A; } #world_borders_1 [ pop2005 <= 5416945] { polygon-fill: #FD8D3C; } #world_borders_1 [ pop2005 <= 2253501] { polygon-fill: #FEB24C; } #world_borders_1 [ pop2005 <= 456613] { polygon-fill: #FED976; } #world_borders_1 [ pop2005 <= 67827] { polygon-fill: #FFFFB2; }',
interactivity: 'cartodb_id'
}]
})
.addTo(map)
.done(function(layer) {
cdb.vis.Vis.addInfowindow(map, layer.getSubLayer(0), ['cartodb_id'])
my_layer = layer.getSubLayer(0);
var sql = new cartodb.SQL({ user: 'iriberri' });
setInterval(function(){
//You need to retrieve the value you're interested in
sql.execute("SELECT color FROM world_data_marker WHERE country = 'Spain'").done(function(data){
var value = data.rows[0].color;
//With the values, you can then build your own custom CSS
var query = "#world_borders_1{ polygon-opacity: 0.8; line-color: #FFF; line-width: 0.5; line-opacity: 1; } #world_borders_1 [ pop2005 <= 1312978855] { polygon-fill: #B10026; } #world_borders_1 [ pop2005 <= 32270507] { polygon-fill: #E31A1C; } #world_borders_1 [ pop2005 <= 10528226] { polygon-fill: #FC4E2A; } #world_borders_1 [ pop2005 <= 5416945] { polygon-fill: #FD8D3C; } #world_borders_1 [ pop2005 <= 2253501] { polygon-fill: #FEB24C; } #world_borders_1 [ pop2005 <= 456613] { polygon-fill: #FED976; } #world_borders_1 [ pop2005 <= 67827] { polygon-fill: #FFFFB2; } #world_borders_1 [name=\"Spain\"] { polygon-fill: "+value+"; }";
//You can then apply the CartoCSS to the layer
my_layer.setCartoCSS(query);
});
}, 10000);
});
}
// 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