Click on layer nodes below to change their properties.
  • OpenAerial layer
  • Layer group
    • Food insecurity layer
    • World borders layer

Layer group example

Example of a map with layer group.

Example of a map with layer group.

tilejson, input, bind, group, layergroup
<!DOCTYPE html>
<html>
<head>
<title>Layer group example</title>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://openlayers.org/en/v3.9.0/css/ol.css" type="text/css">
<script src="http://openlayers.org/en/v3.9.0/build/ol.js"></script>

<style>
#layertree li > span {
  cursor: pointer;
}
</style>
</head>
<body>
<div class="container-fluid">

<div class="row-fluid">
  <div class="span6">
    <div id="map" class="map"></div>
  </div>
  <div id="layertree" class="span6">
    <h5>Click on layer nodes below to change their properties.</h5>
    <ul>
      <li><span>OpenAerial layer</span>
        <fieldset id="layer0">
          <label class="checkbox" for="visible0">
            <input id="visible0" class="visible" type="checkbox"/>visibility
          </label>
          <label>opacity</label>
          <input class="opacity" type="range" min="0" max="1" step="0.01"/>
          <label>hue</label>
          <input class="hue" type="range" min="-3.141592653589793" max="3.141592653589793" step="0.01"/>
          <label>saturation</label>
          <input class="saturation" type="range" min="0" max="5" step="0.01"/>
          <label>contrast</label>
          <input class="contrast" type="range" min="0" max="2" step="0.01"/>
          <label>brightness</label>
          <input class="brightness" type="range" min="-1" max="1" step="0.01"/>
        </fieldset>
      </li>
      <li><span>Layer group</span>
        <fieldset id="layer1">
          <label class="checkbox" for="visible1">
            <input id="visible1" class="visible" type="checkbox"/>visibility
          </label>
          <label>opacity</label>
          <input class="opacity" type="range" min="0" max="1" step="0.01"/>
          <label>hue</label>
          <input class="hue" type="range" min="-3.141592653589793" max="3.141592653589793" step="0.01"/>
          <label>saturation</label>
          <input class="saturation" type="range" min="0" max="5" step="0.01"/>
          <label>contrast</label>
          <input class="contrast" type="range" min="0" max="2" step="0.01"/>
          <label>brightness</label>
          <input class="brightness" type="range" min="-1" max="1" step="0.01"/>
        </fieldset>
        <ul>
          <li><span>Food insecurity layer</span>
            <fieldset id="layer10">
              <label class="checkbox" for="visible10">
                <input id="visible10" class="visible" type="checkbox"/>visibility
              </label>
              <label>opacity</label>
              <input class="opacity" type="range" min="0" max="1" step="0.01"/>
              <label>hue</label>
              <input class="hue" type="range" min="-3.141592653589793" max="3.141592653589793" step="0.01"/>
              <label>saturation</label>
              <input class="saturation" type="range" min="0" max="5" step="0.01"/>
              <label>contrast</label>
              <input class="contrast" type="range" min="0" max="2" step="0.01"/>
              <label>brightness</label>
              <input class="brightness" type="range" min="-1" max="1" step="0.01"/>
            </fieldset>
          </li>
          <li><span>World borders layer</span>
            <fieldset id="layer11">
              <label class="checkbox" for="visible11">
                <input id="visible11" class="visible" type="checkbox"/>visibility
              </label>
              <label>opacity</label>
              <input class="opacity" type="range" min="0" max="1" step="0.01"/>
              <label>hue</label>
              <input class="hue" type="range" min="-3.141592653589793" max="3.141592653589793" step="0.01"/>
              <label>saturation</label>
              <input class="saturation" type="range" min="0" max="5" step="0.01"/>
              <label>contrast</label>
              <input class="contrast" type="range" min="0" max="2" step="0.01"/>
              <label>brightness</label>
              <input class="brightness" type="range" min="-1" max="1" step="0.01"/>
            </fieldset>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>

</div>
<script>
var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.MapQuest({layer: 'sat'})
    }), new ol.layer.Group({
      layers: [
        new ol.layer.Tile({
          source: new ol.source.TileJSON({
            url: 'http://api.tiles.mapbox.com/v3/' +
                'mapbox.20110804-hoa-foodinsecurity-3month.jsonp',
            crossOrigin: 'anonymous'
          })
        }),
        new ol.layer.Tile({
          source: new ol.source.TileJSON({
            url: 'http://api.tiles.mapbox.com/v3/' +
                'mapbox.world-borders-light.jsonp',
            crossOrigin: 'anonymous'
          })
        })
      ]
    })
  ],
  target: 'map',
  view: new ol.View({
    center: ol.proj.fromLonLat([37.40570, 8.81566]),
    zoom: 4
  })
});

function bindInputs(layerid, layer) {
  var visibilityInput = $(layerid + ' input.visible');
  visibilityInput.on('change', function() {
    layer.setVisible(this.checked);
  });
  visibilityInput.prop('checked', layer.getVisible());

  $.each(['opacity', 'hue', 'saturation', 'contrast', 'brightness'],
      function(i, v) {
        var input = $(layerid + ' input.' + v);
        input.on('input change', function() {
          layer.set(v, parseFloat(this.value));
        });
        input.val(String(layer.get(v)));
      }
  );
}
map.getLayers().forEach(function(layer, i) {
  bindInputs('#layer' + i, layer);
  if (layer instanceof ol.layer.Group) {
    layer.getLayers().forEach(function(sublayer, j) {
      bindInputs('#layer' + i + j, sublayer);
    });
  }
});

$('#layertree li > span').click(function() {
  $(this).siblings('fieldset').toggle();
}).siblings('fieldset').hide();

</script>
</body>
</html>