Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I activate a feature + popup when clicking outside of a map in Openlayers?

I'm re-parsing the KML that's already been loaded onto the map similar to the example here: http://openlayers.org/dev/examples/sundials.html and turning it into a clickable list that will center the map on the point clicked, and display the popup window for it.

This was really easy to do in Google Maps, but I can't find any similar Openlayers examples. Is there any easier way to do this? Something built-in that I'm missing?

HTML:

<ul id="locationTable">
</ul>

JS:

 htmlRows = "";
 for(var feat in features) {
     // Build details table 
     featId = features[feat].id; // determine the feature ID     
     title = jQuery(f).filter('[name=TITLE]').text();

     htmlRow = "<li><a href="javascript:selectFeature('"+featId+"');\">"+title+"</a></li>";
     htmlRows = htmlRows + htmlRow;
 }
 jQuery('#locationTable').append(htmlRows);

And then for the selectFeature function:

function selectFeature(fid) {
    for(var i = 0; i<kml.features.length;++i) {
                     if (kml.features[i].id == fid)
                         {         
                             selected = new OpenLayers.Control.SelectFeature(kml.features[i]); 
                             selected.clickFeature(); // make call to simulate Click event of feature
                             break;             
                         }
            }

        }
like image 743
Chris Avatar asked Dec 06 '10 21:12

Chris


People also ask

How do I add pop up to OpenLayers?

Click on the map to get a popup. The popup is composed of a few basic elements: a container, a close button, and a place for the content. To anchor the popup to the map, an ol/Overlay is created with the popup container.


1 Answers

I think you should remove the "selected.clickFeature" call, and instead create an event listener for the "featureselected" event in your feature layer:

OpenLayers.Layer.Vector

If you display the popup in that event, you will only have to find it and select it with your existing code, and remove the line selected.clickFeature();

Sidenote: Can your feature server deliver data in other formats? WFS for instance? Parsing KML data shouldn't be needed.

like image 91
Niklas Wulff Avatar answered Sep 22 '22 11:09

Niklas Wulff