One of my students wants to style a WFS layer and have selections too. I got as far as this code http://ian01.geog.psu.edu/geoserver/www/wfs/wfs-sld2.html which styles the WFS (though with out the text labels) and while the selection control seems to be working (i.e. the box is blue not red) nothing is actually selected.


Looks like you're on your way to getting this to work. Though, I know this selection thing can be tricky so I thought i'd share my code which does selection and hover highlighting. Using a control to highlight and a control for selection:

    var handler_featurehighlighted = function (e) {
    var handler_featureunhighlighted = function (e) {
    var handler_featureselected = function (e) {
    var handler_featureunselected = function (e) {

    MyCompany.UI.Map.Results.highlightControl = new OpenLayers.Control.SelectFeature(MyCompany.UI.Map.Results.Layer, {
        hover: true,
        highlightOnly: true,
        renderIntent: "temporary",
        eventListeners: {
            featurehighlighted: handler_featurehighlighted,
            featureunhighlighted: handler_featureunhighlighted

    MyCompany.UI.Map.Results.selectControl = new OpenLayers.Control.SelectFeature(MyCompany.UI.Map.Results.Layer,
            hover: false,
            multiple: true,
            toggle: true,
            eventListeners: {
                featurehighlighted: handler_featureselected,
                featureunhighlighted: handler_featureunselected
            //multipleKey: "shiftKey"



// don't let results cover region
    MyCompany.UI.Map.getMap().raiseLayer(MyCompany.UI.Map.Results.Layer, -2);

You don't need a separate layer in which to select features (unless you want copies of the features). So you can remove the following code:

select = new OpenLayers.Layer.Vector("Selection", {styleMap: 
    new OpenLayers.Style(OpenLayers.Feature.Vector.style["select"])

And the onSelect, onUnSelect functions. Just set the OpenLayers.Feature.Vector.style["select"] on your WFS layer, and the control will take care of the rest.

In addition:

The onSelect function of the select control takes a single feature as a parameter so:

onSelect: function(e){
unSelect: function(e){

should be:

onSelect: function(feature){
onUnselect: function(e){
  • OK so now I can select but unselect doesn't work. – Ian Turton Mar 11 '11 at 21:04
  • @iant - function name should be onUnselect – geographika Mar 11 '11 at 21:07
  • Better - but I only get one select/unselect – Ian Turton Mar 11 '11 at 21:39
  • @iant - the selectable layer should only require a new selection style added to it. The tool takes care of the rest. – geographika Mar 14 '11 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.