-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: mouseenter/mouseleave for individual features #5539
Comments
Hi @bartcorremans, thanks for the detailed and thought-out suggestion! I agree this would be a useful feature.
This is in fact how we would implement it at a library level too. It's similar to how the existing layer-oriented |
Yeah, I run into this fairly often. I always seem to forget that this isn't how The code usually looks like this:
|
→ #6215 |
There is no solution or workaround for this yet? |
See the workaround I posted three comments back. |
@stevage Indeed, thank you, I overlooked the way queryRenderedFeatures is used. Cheers :) |
I have posted about the general meta-problem here, in #13326. |
Motivation
A common use case for interactive maps is doing something on mouseenter/mouseleave of a geojson feature: highlighting the feature, showing a popup at its location (like in the official example), etc.
The problem I currently have with this is that layer mouseenter/mouseleave events are only fired when the cursor enters/leaves the layer, not individual features. This poses an issue when you have several adjacent features in the same layer with no visible gap between them; when moving the cursor over these features, a mouseenter event will only be fired for the first feature the cursor hovers over. This is insufficient when you want to update individual features or attach a popup at their precise location. The same is true for mouseleave, although it's easy to work around this when you get a new mouseenter event from the next feature.
It'd be a big help if we had something along the lines of a "featureenter" and "featureleave" event, which would fire for individual features.
Design Alternatives
Alternatively, the existing layer mouseenter/mouseleave events could be adapted with this new functionality, but it seems more interesting to me to have both separate use cases available.
With the API that is currently available, I'm not sure how I would go about implementing an elegant and performant "feature mouseenter" use case in my applications.
The only thing I can think of is attaching a mousemove listener on layer mouseenter, and every time it fires, checking which feature the cursor is over and comparing it to the previous one, either by requiring features to have a unique identifier or by checking their coordinates, but this seems neither elegant nor performant.
Ignoring mouseenter/mouseleave altogether and only relying on mousemove seems even worse. In the case of the popup example, this would create the weird effect of the popup anchor moving with the cursor.
If there already is a better way to do what I'm trying to achieve, then my apologies! I'd love to hear about alternative solutions.
Mock-Up
map.on('featureenter', layer, listener)
would call the listener with a MapMouseEvent whenever the cursor enters a different feature.map.on('featureleave', layer, listener)
would do the same when the cursor leaves a feature, even if it enters a new feature at the same time.I imagine this could also work with the 2-argument version of
map.on
, where the layer isn't specified.The text was updated successfully, but these errors were encountered: