[OpenLayers-Users] Opening popup from outside the map

James Morley james at jamesmorley.net
Mon Nov 19 07:48:02 PST 2012


Further to my email last night I now have a public facing example I can
share - see e.g. http://www.catchingtherain.com/bikestats/station.php?id=191

If you click on either of the 'popular' or 'longest' tabs to the left
you'll see the links that display the representative popup on the map.
These are the ones that won't close, whereas if you click on a feature on
the map they do.

The rest of the site is undeveloped as yet so I'd appreciate it if this
isn't shared more widely, but thought a live demonstration of the problem
might help.

Thanks, James



On Sun, Nov 18, 2012 at 9:18 PM, James Morley <james at jamesmorley.net> wrote:

> 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?
>
>


-- 
---
James Morley
www.jamesmorley.net / @jamesinealing
www.whatsthatpicture.com / @PhotosOfThePast
www.apennypermile.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20121119/6c4ea27d/attachment-0001.html>


More information about the Users mailing list