Get the style & layer from a Feature on map press
According to following example https://docs.mapbox.com/mapbox-gl-js/example/queryrenderedfeatures/ (Mapbox GL JS, not the React version, however), I expected to identify which is the layer and the style of the clicked features returned by queryRenderedFeaturesAtPoint (MapView).
The "Feature" notion is not described in here. Apparently, this differs from the meaning in Mapbox GL JS, since the "layer", "source" or "sourceLayer" are not returned by the queryRenderedFeaturesAtPoint function.
My use case is to get the icon name of a clicked Feature ; all my data and style are described in the map-style file with many layers and styles conditions. And I would like to use the "onPress" method on the MapView element.
Same issue. Features returned by queryRenderedFeaturesAtPoint are not including features from the map style layers added directly to the map style (e.g. via Mapbox Studio).
To reproduce:
- Add a layer to the map style via Mapbox Studio
- Create a MapView and verify added features are there
- Click a feature and see that it is not returned by queryRenderedFeaturesAtPoint

onPress = async feature => {
console.log('onPress -> MapView', feature)
const point = feature.geometry.coordinates
const features = await this._map.queryRenderedFeaturesAtPoint(point)
console.log(features)
}
We're using queryRenderedFeatures native api and co, to query rendered features. Those only return the features without layer, etc info.
I'm closing this as we can only implement it with upstream support. See https://github.com/mapbox/mapbox-gl-native/issues/7162
@lrpinto i think your issue is a different issue, if i understand correctly, likely an upstream issue too
Makes sense. Yes. I misread the original issue. Had understood it was the feature itself not returned.
For what is worth, and more for anyone else stucking on this...
Even when using ShapeSource and SymbolLayer queryRenderedFeaturesAtPoint didn't return the features added to the style.
To actually get the feature I used the onSymbolLayerPress. And if you really need to add layers via Mapbox Studio then you can still add a transparent layer programaticly just to handle the presses.
As for getting style and the layer of a feature a workaround for this issue that worked for me for example to identify if a feature belongs to layer A say or B:
onPressSymbolLayer = (feature) => this.handleOnPressSymbolLayer(layer, style, feature)
It is not the same as getting it from the feature, but it does the job.
It would not make much sense to get them from the feature directly as a feature as a specific GeoJson specification, and modeling it as an object does not owns a style or layer.
You can though, set those parameters on the feature GeoJson properties using Thurf for example. Or directly on the GeoJson. And then they would be in feature.properties when you get the feature pressed.
Hope it makes sense.
Hi Irpinto.
Thank you for your answer. However, where do you find the onPressSymbolLayer listener specifications?
Hi Sullix,
I am sorry, I meant onPress on ShapeSource, onPressSymbolLayer was just a name I had given in my code.
Let me explain this better starting with a little context. It might work for your case or not...
Problem: First I had all my features in one set and was using 1 shape source only with data driven styles. The issue with this is as you also reported I could not distinguish a click on red cluster from a click on green cluster. As cluster features are generated and not in my GeoJson source, I could not have defined their properties in advance. And as you also say is not possible to get style or layer info from the feature, so I was not able to know whether a 'red' or say 'green' cluster layer had been pressed.
Solution: In order to sort this I broke my features in 6 sets of GeoJson features - red, blue, green, so on and so far. For each set I then have 1 circle layer and 2 symbol layers. These are clusters, cluster text, and markers. As they are in different ShapeSource components I now have 6 different onPress listeners and so can distinguish from one to another.
Implementation: I have a component that holds styles and GeoJson sources for each group of features. Then for each group I my code abbreviated is like this:
const featureCollection = {
red: redJson,
green: greenJson,
...
}
const styles = {
red: redStyle,
green: greenStyle,
...
}
const Layer = props => {
const { color } = this.props
const onSymbolLayerPress = (color, feature) => {
// here I can check whether the feature is a cluster or not
// if not a cluster must be a marker, as cluster only appear when count > 1, and markers only
appear on count === 1
// I use 'color' to determine whether a red or green layer was pressed
// I am not bothered with the actual style of the layer, but on the single property 'color', pretty much everything else on these styles is the same except a different color or maker png. But if i needed the style I could still do styles[color].clusters, or styles[color].markers
}
return (
<React.Fragment>
<MapboxGL.ShapeSource
onPress={(feature) => this.onSymbolLayerPress(color, feature) }
shape={featureCollection[color]} //
// other props
>
{/* Clusters layer */}
<MapboxGL.CircleLayer
style={styles[color].clusters}
filter={['has', 'point_count']}
// other props
/>
{/* Clusters text layer */}
<MapboxGL.SymbolLayer
style={styles[color].clustersText}
filter={['has', 'point_count']}
// other props
/>
{/* Markers layer */}
<MapboxGL.SymbolLayer
style={styles[color].markers}
filter={['!', ['has', 'point_count']]}
// other props
/>
</MapboxGL.ShapeSource>
</React.Fragment>
)
}
// in parent component render
[ 'green', 'red', ... ].map( color => return <Layer color={color} />
Important: Be sure to give unique IDs to all your shape source and layers otherwise they will not render.
I hope this is any use for you. Cheers! :)
I work out a way to get each feature's sourceId & layerId by tapping the map view and add them to feature's property to meet my project, because the original purpose of the library is only "getTouchableSourceWithHighestZIndex" in "MapClickEvent". I'm not on Java, just post the hacking for those who are interested.
List<RCTSource> touchableSources = getAllTouchableSources();
Map<String, List<Feature>> hits = new HashMap<>();
+
+ // Map<String, List<Feature>> hitsByLayer = new HashMap<>();
+ List<Feature> traceableFeatures = new ArrayList();
+
List<RCTSource> hitTouchableSources = new ArrayList<>();
for (RCTSource touchableSource : touchableSources) {
Map<String, Double> hitbox = touchableSource.getTouchHitbox();
@@ -619,10 +623,26 @@ public class RCTMGLMapView extends MapView implements OnMapReadyCallback, Mapbox
hitboxF.set(screenPoint.x - halfWidth, screenPoint.y - halfHeight, screenPoint.x + halfWidth,
screenPoint.y + halfHeight);
- List<Feature> features = mMap.queryRenderedFeatures(hitboxF, touchableSource.getLayerIDs());
- if (features.size() > 0) {
- hits.put(touchableSource.getID(), features);
- hitTouchableSources.add(touchableSource);
+ String[] layerIDs = touchableSource.getLayerIDs();
+ if (layerIDs.length > 0) {
+ for (String layerID : layerIDs) {
+ String[] queryLayerID = new String[1];
+ queryLayerID[0] = layerID;
+
+ List<Feature> features = mMap.queryRenderedFeatures(hitboxF, queryLayerID);
+
+ if (features.size() > 0) {
+ for (Feature feature : features) {
+ feature.addStringProperty("layerId", layerID);
+ feature.addStringProperty("sourceId", touchableSource.getID());
+ traceableFeatures.add(feature);
+ }
+
+ hits.put(touchableSource.getID(), features);
+ // hitsByLayer.put(layerID, features);
+ hitTouchableSources.add(touchableSource);
+ }
+ }
}
}
EDIT: For my native test I was using the new MapboxMaps v10.2.0 cocoa pod and not the Mapbox-iOS-SDK cocoa pod.
I'm pretty sure this is no longer an upstream limitation anymore (at least for iOS, still need to test Android). I created a native iOS app, created a map pointing to a custom style with a couple of points, and tested the queryRenderedFeatures. The xcode debugger showed my custom layer properties I had added in mapbox studio: