8

Is it possible to show layers with Leaflet depending on scaling? For example not show counties until zoom level reaches 10.

1 Answer 1

20

yes you can set the scale in the constructor:

for example:

var layer = L.tileLayer("http://{s}.tile.page.com/map/{z}/{x}/{y}.jpg", {
        minZoom: 1,
        maxZoom: 14
    } ) ;

vectors don't have max/min properties, you could use a zoomend event to trigger adding or removing them, something like:

    map.on('zoomend ', function(e) {
         if ( map.getZoom() > 13 ){ map.removeLayer( vector )}
         else if ( map.getZoom() <= 13 ){ map.addLayer( vector )}
    });
3
  • Thanks. Is this possible with L.polygon, not with L.tileLayer? It is, I just try it. Thanks so much! Commented Jan 15, 2013 at 13:42
  • I have added a part about vectorlayers
    – warrieka
    Commented Jan 15, 2013 at 14:55
  • 1
    Thanks for the answer, it works which is awesome! Just wanted to add that the method is slightly different from warrieka mentioned. Needs to be - map.getZoom() not map.zoom map.on('zoomend ', function(e) { if ( map.getZoom() > 12 ){ map.removeLayer( vector )} else if ( map.getZoom() <= 12 ){ map.addLayer( vector )} });
    – user22029
    Commented Sep 16, 2013 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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