Leaflet js beginner following/adapting the choropleth example. I need to get something like this mapbox map where the feature styles change according to zoom level. I am loading a line geojson that gets style from getColor / getWidth functions related to feature.properties. How do I get the weight to be 1 when currentZoom is <= 16?
var geojson = L.geoJson(roads, { style: style }).addTo(map); function getColor(p) { return p >= 100 ? '#00F7BD' : p >= 40 ? '#24abc9' : p >= 20 ? '#ef561a' : p >= 0 ? '#4D2A2D' : '#feffbd'; } function getWidth(w) { return w > 20 ? 4.8 : w > 17 ? 4.5 : w > 16 ? 4 : w > 15 ? 3.5 : w > 14 ? 2.5 : w > 10 ? 1.5 : w > 0 ? 0.5 : 0; } // map.on('zoomend', function () { var currentZoom = map.getZoom(); function style(feature) { if (currentZoom <= 16) { return { color: getColor(feature.properties.paP), weight: 1, opacity: 0.5, }; } else { return { weight: getWidth(feature.properties.stW), color: getColor(feature.properties.paP), }; } } // }); Second approach inspired by Stephen's answer @Stephen Lead
function style(feature) { var fa; return { color: getColor(feature.properties.paP), weight: fa * getWidth(feature.properties.stW), opacity: 0.6, lineCap: 'square', lineJoin: 'square', }; } map.on('zoomend', function () { var currentZoom = map.getZoom(); if (currentZoom <= 16) { fa = 0.5; } else { fa = 3; } }); 
style: style(feature)