#MapLibre dynamically updating filter array not working

5 messages · Page 1 of 1 (latest)

fair vigil
#

Hi all! Weird issue - I have a function that updates the filter section of a layer in a MapLibre style dictionary. Currently, my code replaces the whole filter array on the relevant layer:
MAP_STYLE.layers[filterIndex].filter = ["all", ...filters];
but I was asked to have it only replace one of the elements in that array so that other future hardcoded filters wouldn't get messed up by it. I got to this:
MAP_STYLE.layers[filterIndex].filter.splice(1, 1, ...filters);
which replaces the relevant element in the array correctly, BUT the features don't update on the map like they do with the first version.
I put it into a Codepen, which you can see here: https://codepen.io/traitortots-the-sasster/pen/gOjjped
The relevant part starts on line 656 of the JS. Line 660 is the working part that replaces more than needs to be replaced, 661 is commented out, that's the version that replaces only the relevant element in the filters array, which should be working but features don't update like they do with 660. It's set up to log the relevant part of MAP_STYLE on initial load and after each change, so you should be able to see the confusing thing where the array is updating the same with either version if you swap which line is commented out. Any help would be much appreciated!

acoustic river
#

can you send an example of how you expect the filter array to look like assuming it works?
also which filter button are u using? 1-family, affordable housing, or both

acoustic river
#

also, when selecting only affordable housing, nothing is shown on the map. is this a bug or a feature?

fair vigil
#

The filter array should look like this when it gets updated by the 1 family checkbox:

#

The affordable housing filter takes away all but one tiny feature lol, so yeah that’s expected behavior