Skip to content Skip to sidebar Skip to footer

Setting Zoom Level For Layers In Leaflet

Continuing the query from here: https://gis.stackexchange.com/questions/340223/leaflet-making-features-gone-when-zoom-out I would like to have some layers completely gone when zoom

Solution 1:

I found one of the solution, that we can explain by shorter and longer (although more practical) way:

According to the example below:

https://gis.stackexchange.com/questions/258515/show-hide-markers-depending-on-zoom-level

We can do sth like this:

  map.on('zoomend', function() {
  if (map.getZoom() <6){
    map.removeLayer(job);//1st geoJSON layer
   }else{
  map.addLayer(job);
   }
    if (map.getZoom() <7){
    map.removeLayer(job2); //2nd geoJSON layer
    }else{
    map.addLayer(job2);
    }
    if (map.getZoom() <8){
    map.removeLayer(job3); //3rd geoJSON layer
    }else{
    map.addLayer(job3);
    }
  });

which is better for us, unlike to shorter one...

  map.on('zoomend', function() {
    if (map.getZoom() <6){
    map.removeLayer(job);//1st geoJSON layer
   }
   if (map.getZoom() <8){
    map.removeLayer(job2);//2nd geoJSON layer
   }
   if (map.getZoom() <10){
    map.removeLayer(job3);//3rd geoJSON layer
   }
   else {
    map.addLayer(job);
    map.addLayer(job2);
    map.addLayer(job3);
    } //all layers are to be switched on, when zoom level reach 10
   });

that can switch all layers back when zoom level reach max value given in the function.


Post a Comment for "Setting Zoom Level For Layers In Leaflet"