I'd like the ability to highlight shapes on a map by drawing a polygon on a map. Essentially very similar to this example - https://www.mapbox.com/mapbox-gl-js/example/using-box-queryrenderedfeatures/ - except instead of drawing a box, I would draw a polygon.
The motivation for this change is simply to achieve what the example above achieves, but with a much more accurate tool. A bounding box is a rather blunt instrument - compared to a polygon - for selecting geographic areas.
The larger motivation is to be able to provide non-technical people (such as subject matter experts) a means to provide visual feedback on a map, simply and accurately (and for that means to be simple to implement).
I've built a Feedback tool using Mapbox GL JS and Mapbox GL Draw (see https://github.com/charliedotau/mapbox-gl-js-draw-github-gist) to facilitate easy user feedback on map content. Whilst its great for the user - simple to use - its painful for the map editor (who has to take the GeoJSON in the Gist and run intersect queries against the source map to derive the desired data.
AFAIK, the GL Draw API only allows me to get data about the shape drawn by the user, as opposed to data (e.g properties) of the features on the map under the shape drawn by the user.
I know of no other way to achieve the desired outcome using Mapbox.
The UI required for this is the ability to draw a polygon on map (much like Mapbox GL Draw).
n/a - Draw GL provides all that is needed.
n/a
I imagine the implementation is two-fold:
1) extending the QueryRenderedFeatures() method to take a polygon as a parameter and
2) some UI facility to draw the polygon
@charliedotau Thanks for this request, and for explaining your use case!
Using a library like Turf, you should be able to put this feature together by:
turf.bbox to get the bounding box of the polyqueryRenderedFeatures using that bounding boxturf.intersectI'm closing for now, but please do reopen if this doesn't adequately address the issue
thanks @anandthakker. I hadn't thought of that!
I did a little demo - see https://github.com/charliedotau/mapbox-gl-js-select-features-by-draw
thanks
@charliedotau & @anandthakker - this just made my week.
@charliedotau - may I suggest you submit a pull request to add this as an mapbox gl js example. Judging off the stackoverflow activity, I have a feeling it would be helpful to many others.
I recycled this logic to implement the same use case on a feature collection > single polygon added to the map on the click of a button against a feature collection > multiple polygons added once the map is loaded. Works :ok_hand:
UPDATE #1: Actually, this method only draws a box that connects the points of the northeast & southwest box, but does not follow the edges of an irregular polygon. So, it's almost doing what I need, but not exactly. I am working on tweaking so my use case is achieved and will share.
UPDATE #2: My project is webpack + vue.js based and I'm actually getting the incorrect (more than expected number) intersecting polygons when running npm run dev, but the correct (expected number) of intersecting polygons running npm run build. :stuck_out_tongue_closed_eyes: :stuck_out_tongue_closed_eyes:
I'm going to test my code base in pure html + js using the SimpleHTTPServer to serve it and see what happens.
Hi @charliedotau & @anandthakker, are there any updates on using queryRenderedFeatures passing a polygon? like this:
var coords = turf.polygon(e.features[0].geometry.coordinates);
var features = map.queryRenderedFeatures(coords, { layers: ['layer_name'] });
Thanks a lot,
Luca
Most helpful comment
Hi @charliedotau & @anandthakker, are there any updates on using queryRenderedFeatures passing a polygon? like this:
var coords = turf.polygon(e.features[0].geometry.coordinates);var features = map.queryRenderedFeatures(coords, { layers: ['layer_name'] });Thanks a lot,
Luca