1

I'm having trouble getting all intersecting features in the overlapping polygons.

What I want to do is when user clicked on the map it will display all features on the clicked coordinate I already accomplished this but the problem is It will also capture the near polygons even though it is not overlapping to each other.

My current code looks like this:

map = new L.Map('map_div', { center: new L.LatLng(x, y), zoom: 9 });

map.on('click', clickHandler);

function clickHandler(e) {

    var clickBounds = L.latLngBounds(e.latlng, e.latlng);
    var intersectingFeatures = [];
    for (var l in map._layers) {
        var overlay = map._layers[l];
        if (overlay._layers) {
            for (var f in overlay._layers) {
                var feature = overlay._layers[f];
                var bounds;
                if (feature.getBounds) bounds = feature.getBounds();
                else if (feature._latlng) {
                    bounds = L.latLngBounds(feature._latlng, feature._latlng);
                }
                if (bounds && clickBounds.intersects(bounds)) {
                    intersectingFeatures.push(feature);
                }
            }
        }
    }
    var html = intersectingFeatures.map(function (o) {
        try {
            return '<pre>' + JSON.stringify(o.feature.properties, null, ' ').replace(/[\{\}"]/g, '') + '</pre>';
        } catch (err) {
            console.log("no feature found");
            return '';
        }
    }).join('<br />');

    map.openPopup(html, e.latlng, {
        offset: L.point(0, -24)
    });
}

Output:

Map Output

Yuu
  • 619
  • 3
  • 13
  • 34
  • That is because the layer bounds is a rectangle area. See https://stackoverflow.com/questions/43007019/leaflet-event-how-to-propagate-to-overlapping-layers/43011920?r=SearchResults#43011920 – ghybs Mar 16 '21 at 04:06
  • @ghybs Thank you. I tried to solve it using the link you send and it saves me. – Yuu Mar 16 '21 at 08:20

0 Answers0