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

Fix example not changing popup when mouse moves to another overlapping feature #5442

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions test/examples/popup-on-hover.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,41 @@
closeOnClick: false
});

map.on('mouseenter', 'places', (e) => {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
// Make sure to detect marker change for overlapping markers
// and use mousemove instead of mouseenter event
let currentFeatureCoordinates = undefined;
map.on('mousemove', 'places', (e) => {
if (e.features && e.features.length > 0) {
HarelM marked this conversation as resolved.
Show resolved Hide resolved
const featureCoordinates = e.features[0].geometry.coordinates.toString();
if (currentFeatureCoordinates !== featureCoordinates) {
currentFeatureCoordinates = featureCoordinates;

const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';

// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;

// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}

// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates).setHTML(description).addTo(map);
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates).setHTML(description).addTo(map);
}
}
});

map.on('mouseleave', 'places', () => {
currentFeatureCoordinates = undefined;
HarelM marked this conversation as resolved.
Show resolved Hide resolved
map.getCanvas().style.cursor = '';
popup.remove();
});
});
</script>
</body>
</html>
</html>
Loading