简体   繁体   中英

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

How should i call leafletPip.pointInLayer with such data? 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. It is trivial to build one from your GeoJSON data ( L.geoJson(myGeoJSONdata) ). Actually, it even just needs an L.LayerGroup .

Then, it seems that Leaflet-PIP does not handle nested Layer Groups.

Your data is made of MultiPolygons, which Leaflet converts into groups of L.Polygon within the L.GeoJSON group. Therefore Leaflet-PIP does not process them.

So you just have to "flatten" your group before being able to use it correctly with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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