juanignaciosl
1/7/2015 - 11:46 AM

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Leaflet multilayer 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.11/themes/css/cartodb.css" />
  </head>
  <body>
    <div id="map"></div>
    
    <!-- include cartodb.js library -->
    <script src="http://libs.cartocdn.com/cartodb.js/v3/3.11/cartodb.uncompressed.js"></script>
    <script>

      function main() {

        // create leaflet map
        var map = L.map('map', { 
          zoomControl: false,
          center: [43, 0],
          zoom: 3
        })

        // add a base layer
        L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{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);

        // add cartodb layer with one sublayer
        cartodb.createLayer(map, {
            user_name: 'rochoa',
            type: 'namedmap',
            options: {
                named_map: {
                    /* this is the name you get when you create the template:
                    curl -X POST \
                       -H "Content-Type: application/json" \
                       -d @template.json \
                       "https://rochoa.cartodb.com/api/v1/map/named?api_key=${CDB_API_KEY}"

                    #response:
                    {"template_id":"rochoa@world_borders"}
                    */
                    name: 'rochoa@world_borders',
                    // params have to be defined in the placeholders, see template_params.json
                    // and the be inside the style or the sql query, in this case: <%= color %>
                    params: {
                      "color": "#5CA2D1"
                    }
                }
            }
        })
        .addTo(map)
        .done(function(layer) {
            // later on we change the color
            setTimeout(function() {
                layer.setParams({
                    color: '#FF6600'
                });
            }, 5000);
        });
      }

      // you could use $(window).load(main);
      window.onload = main; 

    </script>

  </body>
</html>
{
  "version": "0.0.1",
  "name": "world_borders",
  "auth": {
    "method": "open"
  },
  "placeholders": {
    "color": {
      "type": "css_color",
      "default": "#FF6600"
    }
  },
  "layergroup": {
    "version": "1.0.1",
    "layers": [
      {
        "type": "cartodb",
        "options": {
          "cartocss_version": "2.3.0",
          "cartocss": "#layer { polygon-fill: <%= color %>; polygon-opacity: 1; line-color: #FFF; line-width: 1; line-opacity: 1; }",
          "sql": "select * from world_borders"
        }
      }
    ]
  }
}