Can I access a map instance To get features around a clicked point on map

I want to access my map instance to use methods not exposed thru the plugin currently.

Particularly I want to click on a heatmap that has featurecollections and return those features within x pixels of my click. In the mapbox.js docs they say to using something like this


map.on('click', (e) => {
// Set `bbox` as 5px reactangle area around clicked point.
const bbox = [
[e.point.x - 5, e.point.y - 5],
[e.point.x + 5, e.point.y + 5]
];
// Find features intersecting the bounding box.
const selectedFeatures = map.queryRenderedFeatures(bbox, {
layers: ['counties']
});


//do more stuff with the selectedFeatures
});

But I can’t find the method queryRenderedFeatures. So I was trying to access the map instance to do it myself via code.

Is that possible?

@Alexis do you know if this is possible?

I’m not sure you can for now. But should be a very nice improvement for sure.

I’m thinking about the best way to achieve it. We could add the instance to to click event payload so you can use it, but maybe we could do something else.

We recently released a new feature called component action allowing developers to expose methods like this one.

Maybe we could add a getMapInstance method so you can call it also when you’re triggering a workflow outside of the component, from an external button for example.

Please create a ticket on https://feedback.weweb.io/ so we can prioritize it. You can list others methods or usecase you could be interested too :slight_smile:

2 Likes