简体   繁体   English

是否可以从geojson检查点是否在多边形内?

[英]Is it possible to check if a point is inside a polygon from geojson?

I have a geojson with some polygons, added it as a source and created a layer to show those features on the mapbox map.我有一个带有一些多边形的 geojson,将其添加为源并创建了一个图层以在 mapbox 地图上显示这些特征。

Since I am using this method: (Edit from the sample souce: MapboxGLAndroidSDKTestApp )由于我正在使用此方法:(从示例源编辑: 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?所以我的问题是,有没有办法检查标记是否在多边形内,无论我的相机指向哪里?

To check a point whether lies or not inside a polygon on mapbox can use turf.js library http://turfjs.org/要检查一个点是否位于 mapbox 上的多边形内,可以使用 turf.js 库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 ie turf.inside(point, polygon); // return boolean True/False现在你可以使用来自 turf lib 的 inside 方法,即turf.inside(point, polygon); // return boolean True/False turf.inside(point, polygon); // return boolean True/False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM