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 Answers 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){

  • 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, 2019 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. Mar 14, 2019 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, 2019 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 } Mar 14, 2019 at 18:20
  • running into a problem -- gonna post a question
    – ziggy
    Mar 14, 2019 at 19:21

If you want to role your own intersects algorithm without worrying about turf, you can just grab the code from here: https://stackoverflow.com/questions/31790344/determine-if-a-point-reside-inside-a-leaflet-polygon

Mine looks like:

function isPointInsideMarker(point, poly) {
    var polyPoints = poly.getLatLngs()[0];
    var x = point.lat, y = point.lng;

    var inside = false;
    for (var i = 0, j = polyPoints.length - 1; i < polyPoints.length; j = i++) {
        var xi = polyPoints[i].lat, yi = polyPoints[i].lng;
        var xj = polyPoints[j].lat, yj = polyPoints[j].lng;

        var intersect = ((yi > y) != (yj > y))
            && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
        if (intersect) inside = !inside;

    return inside;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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