Leaflet: L.Popup doesn't fire 'popupclose' event when using multipolygon features

Created on 15 May 2013  路  7Comments  路  Source: Leaflet/Leaflet

Not sure what the logic is, but I have a GeoJSON layer which I switched from a standard polygon dataset to an aggregate (multipart / multipolygon) polygon dataset. I have a listener for popupclose:

                    popup.on('popupclose', function(e) {
                        ...
                    });

... which stopped working when I made the switch, and worked fine again when I switched back.

No errors are thrown to the user when it fails, the event just doesn't get fired.

Firefox / Windows / v0.6 beta (downloaded 8th May), and the problem also occurs in IE8. In 0.5.1 neither works, so I couldn't create a working demo in JSFiddle, but if you paste the code below into a test website and alternate between the two GeoJSON examples, you should see what I mean:

<link rel="stylesheet" href="libs/leaflet/leaflet.css">
<script src="libs/leaflet/leaflet.js"></script>
<div id="map" style="height: 300px"></div>
<script>
var map = new L.Map("map", {
    center: new L.LatLng(1, 1),
    zoom: 14
});

var geojson = {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[1.001,1.001],[1.001,0.999],[0.999,0.999],[0.999,1.001],[1.001,1.001]]]},"properties":{"works": "My close event works fine..."}}]};
//var geojson = {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[1.001,1.001],[1.001,0.999],[0.999,0.999],[0.999,1.001],[1.001,1.001]]]]},"properties":{"works": "Mine doesn't..."}}]};


var lyr = L.geoJson(geojson, {
    style: { weight : 1 },
    onEachFeature : function(feature, layer) {
        var popup = layer.bindPopup(feature.properties.works);
        popup.on("popupclose", function(e) {
            alert("Hi!");
        });
    }
}).addTo(map);
</script>
bug

Most helpful comment

The event listener is on the map object. Here is what worked for me.

map.on('popupclose', function(e){
console.log('popup closed');
});

All 7 comments

Turned that into a js fiddle, it looks like MultiPolygons don't get turned into the same type of objects

edit: I believe your problem is here https://github.com/Leaflet/Leaflet/blob/master/src/layer/GeoJSON.js#L204-L224

This also affects bare L.multiPolygon vector objects, so it's probably not caused by the GeoJSON conversion. Looking into it.

@tmcw perhaps we should add more events there, popupopen at least?

Sure, if so we should make an exhaustive list. Also I just wonder if it's useful to hard-specify all events here versus having a wildcard for this kind of propagation.

@tmcw we could implement wildcard support in events but it's probably not worth it as this propagation is only used in one place, and it's simpler to just explicitly list what events are propagated than to add a rarely used feature to the core.

Okay, propagating popupopen as well.

The event listener is on the map object. Here is what worked for me.

map.on('popupclose', function(e){
console.log('popup closed');
});

Was this page helpful?
0 / 5 - 0 ratings