dimfeld / svelte-maplibre

Svelte bindings for the MapLibre mapping library
https://svelte-maplibre.vercel.app
MIT License
284 stars 34 forks source link

Support layers only firing mouse events if it is the top-most layer for that event. #46

Closed dimfeld closed 10 months ago

dimfeld commented 10 months ago

This gives more intuitive click behavior, since we often want clicks to only apply to the topmost layer which the user can actually see.

This can be done using map.queryRenderedFeatures on the event's point, and then the first result in the returned array is the top-most. Each layer than calls this and compares the first result with itself.

The primary challenge here is to only call this once per event, across all layers. It's potentially expensive to call this multiple times per event, and each call will always return the same results, so some way of caching the result should be present. I'm hoping there's a suitable key accessible to all the layers for an event that we can use in a WeakMap, but need to research a bit. Worst case we just cache keyed on event.point, keeping only the latest, and also autoclear after a tiny delay.

cf #45