question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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>

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
GabrielRamirezcommented, Jul 9, 2019

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

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

1reaction
pouriamoosavicommented, Apr 30, 2021

I am using leaflet 1.7.1 and there is a remove event in popups. I’m not sure if this is the same event as popupclose but it works for me:

L.popup()
  .setContent(`<div>something here</div>`)
  .setLatLng(latlng)
  .openOn(myMap)
  .on("remove", function () {
      console.log("removed. or closed?!");
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my Leaflet popup closing unexpectedly? - Stack Overflow
I believe your animated setView is closing out the popup as part of the moveend event. Instead, try using this code for the...
Read more >
Leaflet ignore click event when popup is open
When the popup is closed the props are shown in a fixed tooltip over the polygon. However, the popup always opens and closes...
Read more >
Documentation - a JavaScript library for interactive maps
Creates a popup with the specified content and options and opens it in the given point on a map. closePopup(<Popup> popup?) this. Closes...
Read more >
L.Polygon - WRLD3D
A class for drawing polygon overlays on a map. Extends Polyline . Note that points you pass when creating a polygon shouldn't have...
Read more >
Breaking changes | ArcGIS Maps SDK for JavaScript 4.25
The following classes, methods, properties and events have been ... Popups using a default template will now display attachments as thumbnail images instead ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found