简体   繁体   English

如何将Leaflet-pip插件与纯GeoJSON数据一起使用?

[英]How to use leaflet-pip plugin with pure GeoJSON data?

I have trouble with using leaflet-pip: i get geojson data from here: http://polygons.openstreetmap.fr/get_geojson.py?id=1320234&params=0 我在使用Leaflet-pip时遇到了麻烦:我从这里获取geojson数据: http ://polygons.openstreetmap.fr/get_geojson.py?id=1320234&params=0

How should i call leafletPip.pointInLayer with such data? 我该如何使用此类数据调用leafletPip.pointInLayer? I have several attempts, but it returns empty array, but i know that there are some markers inside those layers. 我有几次尝试,但是它返回空数组,但是我知道在这些层中有一些标记。

Just in case, note first that Leaflet-PIP needs an L.GeoJSON group to be passed as 2nd argument. 以防万一,首先请注意, Leaflet-PIP需要一个L.GeoJSON组作为第二个参数传递。 It is trivial to build one from your GeoJSON data ( L.geoJson(myGeoJSONdata) ). 从您的GeoJSON数据( L.geoJson(myGeoJSONdata) )中构建一个很简单。 Actually, it even just needs an L.LayerGroup . 实际上,它甚至只需要一个L.LayerGroup

Then, it seems that Leaflet-PIP does not handle nested Layer Groups. 然后,似乎Leaflet-PIP无法处理嵌套的图层组。

Your data is made of MultiPolygons, which Leaflet converts into groups of L.Polygon within the L.GeoJSON group. 您的数据由MultiPolygons的,其单张转换成的组L.Polygon的L.GeoJSON组内。 Therefore Leaflet-PIP does not process them. 因此,Leaflet-PIP不处理它们。

So you just have to "flatten" your group before being able to use it correctly with Leaflet-PIP. 因此,您只需要将组“拉平”,然后才能与Leaflet-PIP正确使用即可。 Parse the group and extract all non-group layers into another group. 解析组并将所有非组图层提取到另一个组中。

var gjLayer = L.geoJson(myGeoJSONdata).addTo(map);

var groupOfNonGroup = L.layerGroup();

function copyToGroupOfNonGroup(group) {
  group.eachLayer(function (layer) {
    if (layer instanceof L.LayerGroup) {
      copyToGroupOfNonGroup(layer);
    } else {
      layer.addTo(groupOfNonGroup);
    }
  });
}

copyToGroupOfNonGroup(gjLayer);

var results = leafletPip.pointInLayer([lng, lat], groupOfNonGroup);

Demo: https://plnkr.co/edit/6hRKHHtvWOVdWg4jZ8AJ?p=preview 演示: https//plnkr.co/edit/6hRKHHtvWOVdWg4jZ8AJ?p = preview

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

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