[GIS] disable onEachFeature at zoom level, leaflet

javascriptleafletzoom

I have a leaflet map that zooms and fits on the bounds of the polygon that is clicked via onEachFeature.

Is there a way to disable this at a certain zoom level? I add new layers on the 'zoomend' event, I just can't figure out if disabling on 'click' function is possible.

function onEachFeature(feature, layer) {
        layer.on('mouseover', function() {
            this.setStyle({
                'fillColor': 'gray'
            });
        });
        layer.on('mouseout', function() {
            this.setStyle({
                'fillColor': '#fff'
            });
        });
        layer.on('click', function() {
            map.fitBounds(layer.getBounds())
        });

    }

var json_countiesJSON = new L.geoJson(json_counties, {
        style: doStylecounties,
        onEachFeature: onEachFeature
    }).addTo(map)

map.on('zoomend ', function(e) {
        if (map.getZoom() < 10) {
            map.removeLayer(clusterWells)
            json_countiesJSON.setStyle({
                'fillOpacity': '0.7'
            });
        } else if (map.getZoom() >= 10) {
            map.addLayer(clusterWells);
            json_countiesJSON.setStyle({
                'fillOpacity': '0.0'
            });
        }
    });

Best Answer

You could – along the lines of map.on('zoomend' … insert a conditional statement into layer.on('click' … querying map.getZoom():

layer.on('click', function() {
        if(map.getZoom() < 10) {
            map.fitBounds(layer.getBounds())
        }
    });