in code veritas in code veritas - 6 months ago 63
Javascript Question

Changing the style of each feature in a Leaflet GeoJSON layer

I have been studying the Leaflet Chloropleth example.

In my Leaflet application, I have a jQuery dropdown that, when selected, fires a function that takes the name of a state as an argument. I want to use that state name to update the Chloropleth map.

What is the pattern for changing the style of a Leaflet GeoJSON layer? Should I recreate the layer I created with

L.geoJson()
a second time? It seems as though the layers are drawing on top of each other with that approach.

I can provide a Fiddle if needed.

Answer

Here's an example of changing a choropleth to classify on different properties - the trick is to call setStyle again with different values.