[OpenLayers-Users] Opening popup from outside the map

James Morley james at jamesmorley.net
Sun Nov 18 13:18:38 PST 2012


Hi, I have written a basic function to allow me to display a popup from a
link outside the map. The basic functionality to open the popup is working
fine, but I can't then close it.

Here's a bit more detail ...

A typical map has about 300 features on a vector layer 'stations' loaded
from kml. These are activated onload using

            select = new
OpenLayers.Control.SelectFeature(stations);
            stations.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
                });
            map.addLayer(stations);
            map.addControl(select);
            select.activate();

which works fine - I can open and close popups.

With my off-map links I am calling onclick="showMyPopup([x]) with [x] being
an ID attribute loaded in from the kml. The showMyPopup function is

function showMyPopup(myID){
    for(var a = 0; a < stations.features.length;
a++){                             //loop through all the features in
stations
    var feature = stations.features[a];
    if (feature.attributes.ID.value == myID)
{                                        //until it finds the one with the
matching ID attribute
       var content = "<h4>" + feature.attributes.name + "</h4>" +
feature.attributes.description;
       popup = new OpenLayers.Popup.FramedCloud("chicken",

feature.geometry.getBounds().getCenterLonLat(),
                                     new OpenLayers.Size(200,200),
                                     content,
                                     null, true, onPopupClose);
       feature.popup = popup;
       map.addPopup(popup);
       }
    }
}

This opens the correct popup from the stations layer as expected, and I can
see the popup using the DOM inspector on the stations layer just as it
would appear if loaded by clicking on the map feature, but there's then
seemingly no way of closing it. The original features on the stations layer
are working fine though (opening and closing). And the final clue is that
if I open a popup from on the map but click the 'x' on a popup still being
displayed from off-map link it closes the other popup. So the bottom line
is that when it calls onFeatureUnselect it doesn't seem to connect with the
popup loaded from the off-map link, if that makes sense!

Any help would be much appreciated (maybe there's a simpler way of tackling
this?)

Thanks, James

PS and just in case, here's the onFeatureUnselect function ...

function onFeatureUnselect(event) {
            var feature = event.feature;
            if(feature.popup) {
                map.removePopup(feature.popup);
                feature.popup.destroy();
                delete feature.popup;
            }
         }

Maybe I should just make that generic so it kills any and all pop-ups?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20121118/092e246e/attachment.html>


More information about the Users mailing list