Skip to content

Instantly share code, notes, and snippets.

@iriberri
Created October 16, 2015 15:28

Revisions

  1. Carla created this gist Oct 16, 2015.
    92 changes: 92 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Annotation element (no ball) | 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: 90%;
    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>

    <button type="button" id="button-basemap1" onclick="setButton1();"> Button 1 (Spain)</button>

    <button type="button" id="button-basemap2" onclick="setButton2();"> Button 2 (Area > 5000)</button>



    <!-- include cartodb.js library -->
    <script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>

    <script>


    var dataLayer;

    function setButton1(){
    //filtering by country name
    dataLayer.setSQL("select * from european_countries_e where name = 'Spain'");

    //changing the css too
    dataLayer.setCartoCSS("#european_countries_e{ polygon-fill: #CCC; polygon-opacity: 0.8; line-color: #FFF; line-width: 1; line-opacity: 0.5; } ");

    }

    function setButton2(){
    //filtering by area
    dataLayer.setSQL("select * from european_countries_e where area > 5000");

    //changing the css, not mandatory!
    dataLayer.setCartoCSS("#european_countries_e{ polygon-fill: #FFFFB2; polygon-opacity: 0.8; line-color: #FFF; line-width: 1; line-opacity: 0.5; } #european_countries_e [ area <= 1638094] { polygon-fill: #B10026; } #european_countries_e [ area <= 55010] { polygon-fill: #E31A1C; } #european_countries_e [ area <= 34895] { polygon-fill: #FC4E2A; } #european_countries_e [ area <= 12890] { polygon-fill: #FD8D3C; } #european_countries_e [ area <= 10025] { polygon-fill: #FEB24C; } #european_countries_e [ area <= 9150] { polygon-fill: #FED976; } #european_countries_e [ area <= 5592] { polygon-fill: #FFFFB2; } ");
    }
    function main() {

    var map = new L.Map('map', {
    zoomControl: false,
    center: [43, 0],
    zoom: 3

    });

    //Adding the basemap here
    var basemap = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{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);


    cartodb.createLayer(map, {
    //change your username below
    user_name: 'documentation',
    type: 'cartodb',
    sublayers: [{
    //change your sql below
    sql: "SELECT e.cartodb_id, e.area, w.subregion, w.un, e.the_geom, e.the_geom_webmercator FROM european_countries_e e LEFT JOIN world_borders w ON e.iso_2_code = w.iso2",
    //change your cartocss below. you can copy it from the CartoDB Editor and remove the line breaks.
    cartocss: "#european_countries_e{ polygon-fill: #FFFFB2; polygon-opacity: 0.8; line-color: #FFF; line-width: 1; line-opacity: 0.5; } #european_countries_e [ area <= 1638094] { polygon-fill: #B10026; } #european_countries_e [ area <= 55010] { polygon-fill: #E31A1C; } #european_countries_e [ area <= 34895] { polygon-fill: #FC4E2A; } #european_countries_e [ area <= 12890] { polygon-fill: #FD8D3C; } #european_countries_e [ area <= 10025] { polygon-fill: #FEB24C; } #european_countries_e [ area <= 9150] { polygon-fill: #FED976; } #european_countries_e [ area <= 5592] { polygon-fill: #FFFFB2; }"
    }]
    })
    .addTo(map)
    .done(function(layer) {

    //here I store the layer in the variable, so that we can query it outside this function
    dataLayer = layer.getSubLayer(0);
    })
    .error(function(err) {
    console.log(err);
    });


    }
    window.onload = main;
    </script>
    </body>
    </html>