Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
      <link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />

</head>
<body>
<div class="row">
          <div id="mapContainer" class="col-lg-12 villages_map">
            <div id="title" class="title">
            <p>Municipios españoles <br> con más de 100 mil habitantes</p>
            </div>
            <div id="map"></div>
            <div id="sql" class="layer_selector villages_selector">
              <p>Selecciona índice</p>
              <ul>
                <li data="larger100k" data-type="sql" class="sql_selected">
                  Municipios con más de 100 mil habitantes
                </li>
                <li data="larger100k_women" data-type="sql">
                  Filtro municipios con mayoría de mujeres
                </li>
                <li data="larger100k_men" data-type="sql">
                  Filtro municipios con mayoría de hombres
                </li>
                <li data="reset" data-type="sql">
                  <button>Reset</button>
                </li>
              </ul>
            </div>
          </div>
</div>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
    <script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js  "></script>

  
</body>
</html>
              
            
!

CSS

              
                html, body, #map {
    height: 500px;
    padding: 0;
    margin: 0;
}
#cartocss {
    position: absolute;
    top: 20px;
    right: 20px;
}
#sql {
    position: absolute;
    top: 2rem;
    right: 1.2rem;
}
.layer_selector {
    background: rgba(255,255,255,0.9);
    border-radius: 5px;
    padding: 0;
    border: 1px solid #999;
    width: 150px;
}

.layer_selector > p {
    padding: 15px 30px;
    border-bottom: 1px solid #999;
    margin:0;
}

.layer_selector ul {
    padding: 0; margin: 0;
    list-style-type: none;
}
.layer_selector li {
    padding: 15px 30px;
    font-family: Helvetica, Arial;
    font-size: 13px;
    color: #444;
    cursor: pointer;
}
.layer_selector li:not(:last-child) {
    border-bottom: 1px solid #999;
}
.layer_selector li:hover {
    background-color: #F0F0F0;
    cursor: pointer;
}
.layer_selector li.sql_selected,
.layer_selector li.cartocss_selected {
    /*background-color: #A6CEE3;*/
    background-color: #ffffff;
}



#mapHeader{
  display:inline-block;
  width:100%;
}


/*Villages styles*/
.villages_map { position:relative;}
.villages_map .title{
  position: absolute;
  z-index: 10000;
  top: 20px;
  left: 50px;
  background-color: rgba(0, 0, 0, 0.701961);
  padding: 10px;
  font-style: italic;
  font-size: 20px;
  color: #ffffff;
}
.villages_map .title p{
  margin:0;
  padding:0;
}
.layer_selector.villages_selector {
  background-color: rgba(255, 224, 102, 0.87);
}
.layer_selector.villages_selector ul {
  position:relative;
}
.layer_selector li[data^="larger100k"]:before {
    content: '';
    position: absolute;
    background-color: #FF6600;
    border-radius: 50%;
    width: 10px;
    height: 10px;
    top: 18px;
    left: 11px;
}
.layer_selector li[data="larger100k_women"]:before {
  background-color: #A53ED5;
  top:94px;
}
.layer_selector li[data="larger100k_men"]:before {
  background-color: #5CA2D1;
  top:185px;
}

.layer_selector li[data="reset"] {
  text-align: center;
  background-color:darkgrey;
}
.layer_selector li[data="reset"] button {
background-color:darkgrey;
  color:#fff;
  font:13px "Helvetica",Arial;
}

.layer_selector p {
  text-align: center;
  background-color:darkgrey;
  color:#fff;
  font:13px "Helvetica",Arial;
}

              
            
!

JS

              
                var sublayer0Shown = true;
      // For storing the sublayers
      var sublayers = [];
      var subLayerShown = {
          larger100k:true,
          larger100k_women:false
      };

      var mapLayerIds = {
          larger100k:0,
          larger100k_women:1,
          larger100k_men:2
      };

      var layerData;

      //Layers SQL sources from CartoDB datasets
      var layerSource = {
        user_name: 'jescacena',
        type: 'cartodb',
        sublayers: [
//              {
//            sql: "SELECT * FROM comarcas",
//            cartocss: '/** simple visualization */ #comarcas{  polygon-fill: #FF6600;  polygon-opacity: 0.7;  line-color: #FFF;  line-width: 0.5;  line-opacity: 1;}'
//          },
        {
          id: "larger100k",
          sql: "SELECT * FROM pueblos_espana where habitantes > 100000",
          cartocss: '/** simple visualization */ #pueblos_espana{  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: 10;  marker-fill: #FF6600;  marker-allow-overlap: true;}'
        },
        {
          id: "larger100k_women",
          sql: "SELECT * FROM pueblos_espana where habitantes > 100000 and mujeres > hombres",
          cartocss: '/** simple visualization */ #pueblos_espana{  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: 10;  marker-fill: #A53ED5;  marker-allow-overlap: true;}'
        },
        {
          id: "larger100k_men",
          sql: "SELECT * FROM pueblos_espana where habitantes > 100000 and hombres > mujeres",
          cartocss: '/** simple visualization */ #pueblos_espana{  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: 10;  marker-fill: #5CA2D1;  marker-allow-overlap: true;}'
        }]
      };

  var init = function() {

      //CartoDB Map centered in Madrid
      var map_object = new L.Map('map', {
          center: [40.4000, -3.6833], // Madrid
          zoom: 4
      });

      // For storing the sublayer
      var sublayer;

      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'
      }).addTo(map_object);

      // Add data layer to your map
      cartodb.createLayer(map_object,layerSource)
        .addTo(map_object)
        .done(function(layer) {

          for (var i = 0; i < layer.getSubLayerCount(); i++) {
            sublayers[i] = layer.getSubLayer(i);
          }

          //Set style for aps1314 initially
          var layerData = getSublayerData('larger100k');
          sublayers[mapLayerIds.larger100k].setCartoCSS(layerData.cartocss);
          layerData = getSublayerData('larger100k_women');
          sublayers[mapLayerIds.larger100k_women].setCartoCSS(layerData.cartocss);
          layerData = getSublayerData('larger100k_men');
          sublayers[mapLayerIds.larger100k_men].setCartoCSS(layerData.cartocss);

          //Initial layers display
          sublayers[mapLayerIds.larger100k].show();
          sublayers[mapLayerIds.larger100k_women].hide();
          sublayers[mapLayerIds.larger100k_men].hide();

          createSelector();

          cartodb.vis.Vis.addInfowindow(map_object, sublayers[mapLayerIds.larger100k], ['poblacion', 'provincia' , 'habitantes', 'mujeres' , 'hombres']);
          cartodb.vis.Vis.addInfowindow(map_object, sublayers[mapLayerIds.larger100k_men], ['poblacion', 'provincia' , 'habitantes', 'mujeres' , 'hombres']);
          cartodb.vis.Vis.addInfowindow(map_object, sublayers[mapLayerIds.larger100k_men], ['poblacion', 'provincia' , 'habitantes', 'mujeres' , 'hombres']);
        })
        .error(function(err) {
          console.log("error: " + err);
        });



  };


  // Create layer selector onclick listeners
  function createSelector(layer) {
    
    var $options = $(".layer_selector").find("li");
    $options.on("click", function(e) {
      var $li = $(e.currentTarget);
      var selected = $li.attr('data');
      if (selected==='reset') {
        $li=$('li[data="larger100k"]');
        selected='larger100k';
      }
      
      var layerData = getSublayerData(selected);
      $options.removeClass('sql_selected');
      if (selected !== "") {
        $li.addClass('sql_selected');
      }

      sublayers[mapLayerIds.larger100k].hide();
      sublayers[mapLayerIds.larger100k_women].hide();
      sublayers[mapLayerIds.larger100k_men].hide();

      setTimeout(function() {
        sublayers[mapLayerIds[selected]].show();
      },50);

      
    });
  }

  function getSublayerData(sublayerId) {
    var layers = layerSource.sublayers;

    for (var i in layers) {
      if(layers[i].id===sublayerId) {
        return layers[i];
      }
    }

    return null;
  }

//Run
init();
              
            
!
999px

Console