3

My leaflet map has two WMS layers with different scales; one of them is a 4cm topographic map. I would like to have both WMS on the map but showing only the topographic map when the zoom of the map is sufficient to read something. Currently I use the minZoom / maxZoom attributes in order to switch when zoom 8 is reached:

var topo4cm =L.tileLayer.wms(topoaddress, {
    service: "WMS", transparent: !1, minZoom: 8, maxZoom: 18
});
var carto=L.tileLayer(cartoaddress, {
        minZoom: 0, maxZoom: 7, zoom:function() {
            var e=map.getZoom();
            return 10>e?"L0"+e: "L"+e
        }
    });

This does what I want: when zoom = 8, the topo map is shown. However the other layer (carto) cannot be manually switched in the panel control. Would there be a way to allow the user to manually switch back to carto?

  • Can you clarify your question. Do you want the user to be able to optionally see the carto layer when the zoom level is > 8? – MappaGnosis Feb 29 '16 at 12:29
  • Exactly. As it is, when I reach level zoom 8 and beyond, the baselayer changes. But since I have also put the layers in a control layer, I can still select 'carto', but obviously there is no more data (since the max zoom is 7). The carto layer would still be relevant to see because both of them are from different dates. Thanks! – Mike Atomat Feb 29 '16 at 12:54
3

You can manually control the visibility of the layer without relying (only) on minZoom and maxZoom, for example:

map.on('zoom', function(){
    var z = map.getZoom();

    if (z > 0 && z < 8) {
        return carto.addTo(map);
    }

    if (someManualControlIsActive) {
        return carto.addTo(map);
    }

    return carto.removeFrom(map);
});
  • Thanks! That's perfect since I also wanted to change some style of the overlay :) – Mike Atomat Feb 29 '16 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.