Skip to content
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

mouseMove event lngLat is offset from mouse pointer at intermediate zoom levels #13322

Open
jackcarter opened this issue Nov 10, 2024 · 3 comments

Comments

@jackcarter
Copy link

jackcarter commented Nov 10, 2024

I have a mapbox app that plots points on a map and lets the user hover to reveal additional info. To detect hover state, I'm using a mousemove event + queryRenderedFeatures, but the event's lngLat doesn't always correspond to the mouse cursor's apparent position. It appears to happen at intermediate zoom levels (approximately zoom "5.5"). Setting the map projection to "mercator" prevents the offset from occurring, but I prefer to keep the default "globe" view if possible.

Is there any way to fix the offset, while still using "globe" projection?

mapbox-gl-js version: 3.7.0

browser: Chrome, Safari, Edge

Steps to Trigger Behavior

  1. Create a map with the default "globe" projection, set to zoom level 5.5
  2. Trigger a mousemove event and get the lngLat
  3. Draw a point at the lngLat coordinates, and notice that it is not aligned with the mouse

Link to Demonstration

I built this debug app to show the offset visually. Mousing over the red dots in the grid should cause a popup to appear. The green-and-red circle shows the mousemove event's point.lngLat coordinates. The green-and-red circle does not track my cursor precisely, which makes it hard to select a specific dot.
https://output.jsbin.com/xewegam/1
mapbox

Expected Behavior

mousemove lngLat is visually aligned with the mouse cursor

Actual Behavior

lngLat is misaligned, with a variable offset that depends on the position of the cursor within the map window

@divyanshuraj751
Copy link

divyanshuraj751 commented Nov 12, 2024

Use querySourceFeatures Instead of queryRenderedFeatures may be this can fix the issue

@jackcarter
Copy link
Author

jackcarter commented Nov 12, 2024

Use querySourceFeatures Instead of queryRenderedFeatures may be this can fix the issue

No, this isn't a solution. It doesn't fix the misalignment between the mouse cursor and the lngLat.

@divyanshuraj751
Copy link

divyanshuraj751 commented Nov 12, 2024

  1. Use screen coordinates for hover detection

map.on('mousemove', (e) => {
const point = map.project(e.lngLat);
const features = map.queryRenderedFeatures(point, { layers: ['your-layer-id'] });
if (features.length) {
//....
}
});

  1. Calculate the distance between the mousemove event’s lngLat and your features, and trigger the popup only if the mouse is close enough to a feature

  2. Adjust Position Dynamically (dynamic offset in the queryRenderedFeatures options to improve alignment)

const point = map.project(e.lngLat);
const padding = 10; // Adjust as needed
const features = map.queryRenderedFeatures([
[point.x - padding, point.y - padding],
[point.x + padding, point.y + padding]
], { layers: ['your-layer-id'] });

if (features.length) {
// Show popup
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants