I am trying to implement a function that will filter multiple GeoJSON layers in Leaflet based on the contents of a text box on the same page.

For instance, a user may write 4 5 7 10 in the textbox - after pressing a 'submit' button, I now want to update the map to include features that have an ID GeoJSON attribute of 4, 5, 7, or 10.

I need to apply this filter across multiple layers as ID's 4, 5, 7, or 10 could exist across multiple layers. As such the function needs to be generic in that it will look at any layer, check if any of the ID's match and pick them up, regardless of the layer name or the geojson variable used to create the layer.

From what i can tell in the documentation the only way to apply filters is to reconstruct the GeoJSON layer from scratch whilst using 'filter:myfilter'. Is there a way to avoid this? Ideally a way that avoids having to remove layers from the map etc.

  • 2
    If you don't want to reconstruct the layers with a filter you could use a style function that shows only the selected IDs, see here: gis.stackexchange.com/questions/75590/…
    – Léo M
    Oct 11, 2019 at 10:12
  • That is a fantastic idea! I'll look in to it.
    – Jamie
    Oct 11, 2019 at 10:18


Your Answer

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

Browse other questions tagged or ask your own question.