[GIS] Openlayers popup not closing

openlayers-2popup

This is a special case, I have many popups attached to objects and they work fine and I am having a problem in only one case.

All layers and popups are handled by select control's onFeatureSelect, but this one is handled by layers featureSelected event.

On opening popup it throws:

Uncaught TypeError: Cannot read property 'lat' of null.

Expanding error shows error on function OpenLayers.Bounds.OpenLayers.Class.determineQuadrant but the popup opens.

On Clicking the close button, the click event is also fired and onPopupClose function is also called ( I have checked it with monitor and monitorEvents in console) but the popup does not close. The popup in other layers is still function normally.

Here is the code:

select_Control = new OpenLayers.Control.SelectFeature([business, schools, healthfacilities, open_space], {
    onSelect: onFeatureSelect,
    onUnselect: onFeatureUnselect,
    click: true
});

open_space.events.on({
    "featureselected": function(e) {
        var PopupPos1 = e.feature.geometry.getBounds().getCenterLonLat();
        popup_content = '<div class="sites-embed-border-on sites-embed sites-embed-full-width" style="width:100%;"><div class="sites-embed-object-title" style="display:none;">sifal ground</div><div class="sites-embed-content sites-embed-type-spreadsheet"><iframe src="https://spreadsheets.google.com/spreadsheet/loadredirect?chrome=false&amp;key=0AkXY0mfPYUxLdE9adGRXZFdlc1RxaHpIREtLbGhEZFE&amp;output=html&amp;pubredirect=true&amp;widget=true" width="100%" height="600" title="sifal ground" frameborder="0" id="193883325"></iframe></div></div>';
        var popup1 = new OpenLayers.Popup.FramedCloud("chicken",
            PopupPos1,
            null,
            popup_content,
            null, true, onPopupClose);

        e.feature.popup = popup1;
        map.addPopup(popup1);
        popup1.setBackgroundColor('#FFBBBB');
        popup1.updateSize();
        popup1.draw();
    }
})

function onPopupClose(e) {
    select_Control.unselectAll();
}

function onFeatureSelect(feature) {
    //debugger;
    var PopupPos1 = new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y)
    popup_content = feature.attributes.name;
    var popup1 = new OpenLayers.Popup.FramedCloud("chicken",
        PopupPos1,
        null,
        popup_content,
        null, true, onPopupClose);
    feature.popup = popup1;
    map.addPopup(popup1);
    popup1.setBackgroundColor('#FFBBBB');
    popup1.draw();
}

function onFeatureUnselect(feature) {
    map.removePopup(feature.popup);
    select_Control.unselectAll();
}

Best Answer

have you tried to remove them with a for-loop?

function onFeatureUnselect(feature) {
select_Control.unselectAll();
feature.popup.destroy();
feature.popup = null; 
for(var i=0;i<map.popups.length;i++)
{
map.removePopup(map.popups[0]);
}
Related Question