-
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
Mouse events not working right on circle layers with sub-properties #9469
Comments
I am also facing the same issue and stuck after the implementation. Any update ? |
I am experiencing the same issue. I suspect this has to do with Mapbox GL rendering nested properties as strings, rather than JSON objects. It appears that even though the circle will render to the canvas with the expected This information can be obtained by creating a map.on('click', 'layer-id', function(e) {
console.log(e.features[0].layer.paint);
} This bug appears to apply to any It would be a great benefit to take advantage of nested properties, so I hope this issue can be sorted out fairly easily. |
I would like to see a solution for this as well. I am also experiencing this issue. |
This is affecting me as well. I am able to somewhat get around it by putting a bigger, nearly transparent clickable circle layer behind the visible marker, but it's definitely not ideal as the radius of that invisible layer is sometimes overlapping with neighbors in order to have a fair chance of getting hit. This affects both on('click') and queryRenderedFeatures interactions. |
A user helpfully pointed out that the clickable area of circles seems to have a right-side bias. E.G., you can't click on the left side of circles, but you can on the right side. Maybe something wrong about the center/radius/origin math? |
Any solution for this yet? |
Just to note that this bug is still manifesting in v3.6.0, with the offset behaviour that @sg-code and @mikejcorey describe. @stepankuzmin et al could you have a look? |
Hey everyone, Unfortunately, GL JS doesn't support compound data types such as arrays and objects (i.e., nested properties) out of the box. See also #7796 (comment):
|
@stepankuzmin this behavior of circle layer mouse events not working properly for me happens WITHOUT any nesting within the JSON. I believe the original poster's use of the term "sub properties" may be confusing things from a terminology perspective, because they also mention The real problem is that hover/click events on circles is not actually representing the area of the circle shown on-screen, which leads to user confusion. I ultimately had to change it out for a symbol layer with a circle symbol to make it work properly/expectedly. However it seems unfortunate to have to reach towards this work-around. Hopefully this clarifies things a bit. |
Yep, apologies for the confusion, this is exactly what I'm experiencing: map.addSource('foo', {
'type': 'geojson',
'data': '…')
}).addLayer({
'id': 'foocircles',
'type': 'circle',
'source': 'foo',
'paint': score_style
}).on('click', 'foocircles', (e) => {
// Get the features under the click event
const features = e.features[0];
const coordinates = e.features[0].geometry.coordinates.slice();
// Extract 'school_id' and 'max_score' from the clicked feature's properties
const schoolId = features.properties.name;
const maxScore = features.properties.max_score;
// Create a popup with the extracted information
const description = `<div><${schoolId}</div><div>${maxScore}</div>`;
// 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;
}
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML(description)
.addTo(map);
})
// Change the cursor to a pointer when hovering over the circles
.on('mouseenter', 'foocircles', () => {
map.getCanvas().style.cursor = 'pointer';
})
.on('mouseleave', 'foocircles', () => {
map.getCanvas().style.cursor = '';
}); The popup works, but the hover and hit area is offset as described above. |
Experiencing the same issue here. Switching to markers added to the HTML DOM is not an option for us, as we render thousands of circles on the map in our project. To address this, I devised a workaround that allows us to continue using the circle layer. The solution involves adding another layer to the canvas, based on the same data source as the visible circles. The circles in the new layer are then adjusted to counteract the offset caused by Mapbox, effectively aligning them with the original positions. map
.addLayer({
id: 'clickable-circles',
type: 'circle',
source: 'circle-source',
paint: {
'circle-radius': 10, // Matching the radius of the visible circles
'circle-color': 'rgba(0, 0, 0, 0)',
'circle-translate': [-15, -15], // Offset for proper click area
},
})
.on('click', 'clickable-circles', (e: MapMouseEvent) => {
...
}) |
mapbox-gl-js version: v1.9.0
browser: Chrome
Steps to Trigger Behavior
Link to Demonstration
https://jsbin.com/zahogipeqo/edit?html,output
Expected Behavior
Mouse events should work as soon as the mouse hits the edge of the circle.
Actual Behavior
The mouse events only work when really close to the center of the circle.
Additional information
I initially noticed this when using a a linear expression, but after simplifying this to simplifying grabbing the sub-property value and using it for the radius, I saw the same issue.
The text was updated successfully, but these errors were encountered: