[GIS] Vector Click Event is not working

featuresopenlayers-2vector

Actually I figured out some problems and corrected it. I declared the overlay just outside the init function. So now I got the 'featureselected' event working properly. But 'featureunselected' event is still not working.

//Vehicle Overlay
      var  vmarkers = new OpenLayers.Layer.Vector("Vehicle Overlay",
                {
                    eventListeners: {
                        'featureselected': function(evt) {
                            var feature = evt.feature;
                            var popup = new OpenLayers.Popup.FramedCloud("popup",
                                  OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
                                    null,
                                    "/*pop up content goes here*/",
                                    null,
                                    true
                                    );
                            feature.popup = popup;
                            map.addPopup(popup);
                        },
                        'featureunselected': function(evt) {
                            alert("unselected");
                            var feature = evt.feature;
                            map.removePopup(feature.popup);
                            feature.popup.destroy();
                            feature.popup = null;
                        }
                    }});
    function init()
       {
        map.addLayers([gmap_streets, gmap_hybrid, vmarkers]);
        selectControl = new OpenLayers.Control.SelectFeature(vmarkers, {
                click: true,
                autoActivate: true
            });
            map.addControl(selectControl);
         }

Best Answer

did you already add and activate a SelectControl?

    var selectControl = new OpenLayers.Control.SelectFeature(vmarkers, {});

    map.addControl(selectControl);
    selectControl.activate();

http://jsfiddle.net/expedio/kop5qonq/

Apart from that you have one curly bracket too much between your two eventListeners.