3

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'
            });
        }
    });

1 Answer 1

6

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())
        }
    });
1
  • 2
    Indeed, so simple. Thanks! Classic case of over thinking.
    – Tangnar
    Jul 15, 2015 at 17:19

Your Answer

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

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