Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active May 27, 2016 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramiroaznar/f13e9b025e4e2a2e72958bfbcc6dbed8 to your computer and use it in GitHub Desktop.
Save ramiroaznar/f13e9b025e4e2a2e72958bfbcc6dbed8 to your computer and use it in GitHub Desktop.
CreateLayer and set CartoCSS + SQL
<!DOCTYPE html>
<html>
<head>
<title>CreateLayer and set CartoCSS + SQL</title>
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<!-- include cartodb css -->
<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>
</head>
<body>
<!-- define a map object-->
<div id="map"></div>
<style type="cartocss/text" id="citiesStyle">
/** simple visualization */
#populated_places{
marker-fill-opacity: 0.9;
marker-line-color: #FFF;
marker-line-width: 1;
marker-line-opacity: 1;
marker-placement: point;
marker-type: ellipse;
marker-width: 5;
marker-fill: #000000;
marker-allow-overlap: true;
}
</style>
<style type="cartocss/text" id="countriesStyle">
/** simple visualization */
#world_borders{
polygon-fill: #5CA2D1;
polygon-opacity: 0;
line-color: #5CA2D1;
line-width: 2;
line-opacity: 1;
}
</style>
<script>
function main() {
// create leaflet map and define some properties
var map = L.map('map', {
zoomControl: false,
center: [43, 0],
zoom: 4
})
var citiesStyle = $("#citiesStyle").text();
var countriesStyle = $("#countriesStyle").text();
// add a base layer to map
L.tileLayer('http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png', {
attribution: 'CartoDB'
}).addTo(map);
// add cartodb layer with one layer
cartodb.createLayer(map, {
user_name: 'ramirocartodb', // Required
type: 'cartodb', // Required
sublayers: [
{ // first sublayer is the one that is painted at the bottom
sql: "SELECT * FROM world_borders", // Required
cartocss: countriesStyle
},
{ // second sublayer is painted above the previous sublayer
sql: "SELECT * FROM populated_places", // Required
cartocss: citiesStyle
}
]
}).addTo(map)
.done(function(layer){
var layer0 = layer.getSubLayer(0); // declare a layer0 variable
console.log(layer0); // show in the console layer0
var layer1 = layer.getSubLayer(1); // declare a layer1 variable
console.log(layer1); // show in the console layer1
});
}
// load main() function
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment