3
votes

I have a geojson with some polygons, added it as a source and created a layer to show those features on the mapbox map.

Since I am using this method: (Edit from the sample souce: MapboxGLAndroidSDKTestApp)

final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
                    List<Feature> fList = mapboxMap.queryRenderedFeatures(pixel, "polygons");
                    for(Feature feature:fList){
                        Log.d(TAG,"FeatureInfo: "+feature.toJson());
                    }

I can check if the marker is inside one of those polygons by passing the marker position to it.

But when I move my camera far away to the marker, the polygon is not rendered and the checker is not work anymore.

So my question is, is there any way to check if the marker is inside the polygon, which no matter where is my camera pointing at?

2

2 Answers

5
votes

To check a point whether lies or not inside a polygon on mapbox can use turf.js library http://turfjs.org/

This is the way in which you don't need to plot point or polygon on the mapbox(without plotting).

var point = turf.point([-75.343, 39.984]); 
// here first is lng and then lat 
var polygon = turf.polygon([[
  [-2.275543, 53.464547],
  [-2.275543, 53.489271],
  [-2.215118, 53.489271],
  [-2.215118, 53.464547],
  [-2.275543, 53.464547]
]], { name: 'poly1'});
// here first is lng and then lat 

now you can use inside method from turf lib i.e turf.inside(point, polygon); // return boolean True/False