Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a popup box to a vector in OpenLayers?

In a previous version of my program I used markers to mark points on the map. In the current version I had to change from markers to vectors, because I need the extra flexibility. In the markers solution I used the function below to add a popup-box to a marker:

function createPopupBoxFeature(vector, lonLat, description) {
    var feature = new OpenLayers.Feature(vector, lonLat);

    feature.closeBox = true;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, 
        { "autoSize": true });
    feature.data.popupContentHTML = description;

    vector.events.register("mousedown", feature, function(evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        OpenLayers.Event.stop(evt);
    });

    return feature;
}

But it is no longer working for vectors, because they have no events property. How do I fix this?

like image 729
xsl Avatar asked Sep 17 '11 16:09

xsl


1 Answers

Actually the official way of doing it is the following:

(Note: some of the variables have not been declared in these snippets: longt, lat, map)

http://dev.openlayers.org/examples/light-basic.html

//Step 1 - create the vector layer
var vectorLayer = new OpenLayers.Layer.Vector("ExampleLayer",{
    eventListeners:{
        'featureselected':function(evt){
            var feature = evt.feature;
            var popup = new OpenLayers.Popup.FramedCloud("popup",
                OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
                null,
                feature.attributes.message+"<br>"+feature.attributes.location,
                null,
                true,
                null
            );
            popup.autoSize = true;
            popup.maxSize = new OpenLayers.Size(400,800);
            popup.fixedRelativePosition = true;
            feature.popup = popup;
            map.addPopup(popup);
        },
        'featureunselected':function(evt){
            var feature = evt.feature;
            map.removePopup(feature.popup);
            feature.popup.destroy();
            feature.popup = null;
        }
    }
});

//Step 2 - add feature to layer
var p = new OpenLayers.Geometry.Point(longt, lat);
var feature = new OpenLayers.Feature.Vector(
     p.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()),
     {message:'foo', location:'bar'},
     {externalGraphic: '../img/marker.png', graphicHeight: 21, graphicWidth: 16}
);
vectorLayer.addFeatures(feature);

//Step 3 - create the selectFeature control
var selector = new OpenLayers.Control.SelectFeature(vectorLayer,{
    hover:true,
    autoActivate:true
});

//Step 4 - add the layer and control to the map
map.addControl(selector);
map.addLayer(vectorLayer);
like image 107
Hugh Pearse Avatar answered Nov 09 '22 12:11

Hugh Pearse