block by andrewxhill 7c1043f5fcfc0cfe2d3c

Toggle different vis.json layers on a map

Full Screen

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Leaflet 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="//cartodb.com/assets/favicon.ico" />
    <style>
      html, body, #map {
        height: 100%;
        padding: 0;
        margin: 0;
      }
    </style>
    <link rel="stylesheet" href="//libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" />
  </head>
  <body>
    <div id="map"></div>
    
    <!-- include cartodb.js library -->
    <script src="//libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>
    <script src="//d3js.org/queue.v1.min.js"></script>

    <script>
      function main() {
        var map = new L.Map('map', {
          zoomControl: false,
          center: [43, 0],
          zoom: 3
        });

        L.tileLayer('//tile.stamen.com/toner/{z}/{x}/{y}.png', {
          attribution: 'Stamen'
        }).addTo(map);

        var layers = [
          '//documentation.cartodb.com/api/v2/viz/2b13c956-e7c1-11e2-806b-5404a6a683d5/viz.json',
          '//documentation.cartodb.com/api/v2/viz/236085de-ea08-11e2-958c-5404a6a683d5/viz.json'
          // add here more layers
        ]

        var layerManager = [];

        var q = queue(3);

        layers.forEach(function(vizjson) {
          q.defer(function(vizjson, callback) {
            cartodb.createLayer(map, vizjson, function(layer) { callback(null, layer); })
          }, vizjson);
        })

        q.await(function() {
          var leafletLayers = Array.prototype.slice.call(arguments, 1);
          leafletLayers.forEach(function(lyr) {
            layerManager.push(lyr)
            lyr.addTo(map);
          });
        })

        // the default state of the map. I've chosen it to mean, both layers visble
        var state = 0;
        // here, i'm not linking to any menu click, just any click on the browser
        // you can adapt
        $('body').click(function(){
          switch(state){
            case 0:
              // All layers are on, so move to state 1
              state = 1;
              // State 1 just means, hide the first layer
              layerManager[0].hide();
              break;
            case 1:
              // we are in state 1, so go to state 2
              state = 2;
              // state 2 means, hide layer 2 and show again layer 1
              layerManager[1].hide();
              layerManager[0].show();
              break;
            default:
              // we aren't in 0 or 1, so must be 2. go back to state 0
              state = 0;
              // in state 0, we'll show both layers
              layerManager[1].show();
              // layer 0 is already showing, so no action needed.
              // layerManager[0].show();
          }
        })
      }

      // you could use $(window).load(main);
      window.onload = main;
    </script>
  </body>
</html>