1

I have multiple layers, say a site location as a marker and a town location as a polygon. I want to be able to click on the marker and push it's attributes to a dialog, and also send the polygon's name attribute to the same dialog.

So I need to get both layers information from the same click. The marker is easy and in it's onEachFeature I can run an on-click function, but how do I fire off the click for the second layer by only coordinates?

End result would be a dialog with a tab for each layer, to show it's information.

2

I figured this out, in the marker onEachFeature I added an on-click, and grabbed the clicks coordinates, then I used these coordinates, passed them to a function and used TURF to check if the point coords were inside the municipal poly, if so set the name to a variable and use it to add to my bootstrap dialog. The only issue was turf points use Long, Lat instead of Leaflets lat, long.

        var ptcoords = e.latlng;
        var lat = e.latlng.lat;
        var lng = e.latlng.lng;
        var pt = turf.point([lng,lat]);

    pws.eachLayer(function (layer) {

        isInside =turf.inside(pt, layer.toGeoJSON());

        if (isInside){
            selPoly.push(layer.feature);
            console.log(layer.feature.properties.PWS_Name);
        }

    })
  • cool - but lets say if I want to test if the point is in only in 1 polygon of multipolygon layer...how would only return the info from that one polygon? – ziggy Mar 14 at 12:56
  • Not sure it can be done, as a multipolygon may be 2 or more polygons acting as one, with one record. Just say you have a parcel, split by a road, it's one parcel but 2 pieces, only one record, owner, parcelID, etc.. How would the point be able to tell it's this side or that? Same attributes, you would need to explode them to just polygons, then they would have separate ID's. – Bill Chappell Mar 14 at 13:08
  • sorry my explanation was not what I meant -- scenario: 1 layer with two separate polygons. the point only falls in one of the polgyons..how do you only return that one polygon – ziggy Mar 14 at 13:31
  • With a point layer and polygon layer. nested loop, loop through polygons, and loop through points. var intersection = turf.intersect(myPt.toGeoJSON(), myPoly.toGeoJSON()); if (intersection != undefined){ // must be inside } – Bill Chappell Mar 14 at 18:20
  • running into a problem -- gonna post a question – ziggy Mar 14 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.