-
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
Track popups to mouse cursor #7777
Comments
I think this would be pretty similar to the draggable marker code, the meat of which is https://github.com/mapbox/mapbox-gl-js/blob/master/src/ui/marker.js#L371-L375 It's also important to account for any offset between the cursor position and the position of the element being moved around the screen which is calculated here https://github.com/mapbox/mapbox-gl-js/blob/master/src/ui/marker.js#L441 Another use case for this would be working with multiple, stacked fill extrusions and querying them to show a popup with information as you move to various "floors" of the stacked extrusions. Could be useful for things like real estate apps or terrain applications with raster-dem tiles. |
@ryanhamley exactly, though can we short-circuit the |
You mean because there wouldn't be a LatLng for the popup in the case where it's following the cursor? I think that makes sense. I just wonder if leaving latLng undefined or null would cause unexpected issues elsewhere. It's definitely something you could explore. My guess is the main argument against cutting out the conversion to/from LatLng in this case is one of consistency within the API. |
mouse cursor popup I do this way: popup(event) {
const canvas = this.map.getCanvas();
const node = document.querySelector('#map-popup');
if (event) {
canvas.style.cursor = 'pointer';
node.textContent = event.features[0].properties.title;
node.style.left = `${event.originalEvent.clientX}px`;
node.style.top = `${event.originalEvent.clientY}px`;
node.style.display = 'block';
} else {
canvas.style.cursor = '';
node.style.display = 'none';
node.textContent = '';
}
} this.map.on('mousemove', layer, event => this.popup(event));
this.map.on('mouseleave', layer, () => this.popup()); Using CSS you can center it with |
Interesting, I always do it external to GL JS. Where
Then I watch the GL JS Though I'm 👍 for making this a one-liner in GL JS core, it's a common use case. |
Y'all (@andrewharvey and @Bravecow) can just follow mapbox's guide to display a popup on hover, but set the popup's lngLat to the mapbox event's lngLat property.
Should this be included as a hint in the official example, rather than a completely new implementation? |
@peterqliu your PR for this fix just needs to have a couple of conflicts resolved and then it can be merged and this issue can be closed |
#7786 🚢 |
Motivation
I've seen many use cases (in Studio's data inspector, in my own demos, and from others) where we'd want a popup "inspector" to follow the mouse, rather than any static lnglat. I've usually built these from scratch, but would be ideal to extend the current popup API for this.
Design Alternatives
Can simulate this with the current popup by detecting mouse position on every
mousemove
, projecting that to lnglat, andsetLngLat
to the popup. Onmouseout
andmouseenter
of the map element, the popup would hide and show itself, respectively.But it would be nice to one-line this.
Design/Mockup
Some API ideas:
popup.setLngLat({trackCursor: true})
: would expand the current method accept object literalspopup.trackCursor()
: would add a new method to the Popup class, that along withsetLngLat
would mutually overwrite each other.setLngLat
is not called. May be the easiest to implement, but weirdest to use in practice.The text was updated successfully, but these errors were encountered: