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. Commented Jul 15, 2015 at 17:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.