Amit Gore
Amit Gore

Reputation: 451

How to get the feature geojson from the result of queryRenderedFeatures on a layer whose source is a vector tile in mapbox gl js?

I have a layer called "Searched LayerX" having a vector tile source. I am having a simple requirement of highlighting a feature inside this "Searched LayerX" at runtime.

I was thinking of using the result of queryRenderedFeatures on "Searched LayerX" with the filter of unique ID of this particular feature and using this feature's geojson as a separate source to the new layer which I will be adding as "Selected LayerX".

var features = mapBox.queryRenderedFeatures({layers:['Searched LayerX'], filter : ["==",'gid','7818_2_CA']})
var selectedFeature = features[0];

Resultant feature set does not provide any geojson which I can use to create a new geojson source. So my question is, how do I use the result as a different source to my "Selected LayerX"?

Upvotes: 0

Views: 1370

Answers (2)

Asrar
Asrar

Reputation: 495

Try this post, I have added the code which will let you have the features using querySourceFeatures() https://stackoverflow.com/a/66308173/9185662

Upvotes: 0

Malcolm
Malcolm

Reputation: 473

You can use the method described in the first link below - but understand that the returned feature is not the same as the source GeoJSON feature - it is the vector tile representation of that feature at that zoom level, which means it might be highly simplified.

https://gis.stackexchange.com/questions/186533/highlight-feature-with-click-in-mapbox-gl

Another method is to add another layer with the same source, and use the filter function for the highlight as shown in the two links below -

http://www.mapbox.com.s3-website-us-east-1.amazonaws.com/mapbox-gl-js/example/query-similar-features/

highlighting polyline features in mapbox-gl.js

Upvotes: 1

Related Questions