Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

One of my students wants to style a WFS layer and have selections too. I got as far as this code 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.

share|improve this question
up vote 2 down vote accepted

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);
share|improve this answer
Thanks that was the final clue I needed. – iant Mar 14 '11 at 18:30

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(["select"])

And the onSelect, onUnSelect functions. Just set the["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){
share|improve this answer
OK so now I can select but unselect doesn't work. – iant 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 – iant 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 posting your answer, you agree to the privacy policy and terms of service.

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