1

I'm creating different layers dynamically full of features, everything on-the-fly, and I need to do some stuff when each specific feature is selected. I can't paste the actual source code here as it has dependencies and it's quite long also, but you can imagine something like the following:

if (e.Bubbles) {
     jQuery.each(e.Bubbles, function (i, obj) {
         jQuery.each(obj, function (n, el) {

             /* features and layers ... */
             var point = new OpenLayers.Geometry.Point(el[1],el[0]);

             /* some code here ... */
         });

         layer = new OpenLayers.Layer.Vector(i, {styleMap: e.Style});
         map.addLayer(layer);
         e.Layers.push(layer);

         /* etc. */

When I'm trying to bind the onSelect event to the layer, it works but it replaces the previous layer behavior, so the last layer in the loop works fine when you clicking on the features, but only the last layer. Here is my code (inside the loop):

selectControl = new OpenLayers.Control.SelectFeature(
    Layer,
    {
        clickout: false, 
        toggle: false,
        multiple: false, 
        hover: false,
     }
 );

selectControl.activate();

layer.events.on({
    "featureselected": function(event) {
    console.log(event.feature);
    }
});

So, the selectControl will be replaced inside the loop each time and it will lose its own functionality for the previous ones, and just works for the latest layer.

I tried pass all the layers as an array as well (which is supposed to work) but without any success.

selectControl = new OpenLayers.Control.SelectFeature(
    e.Layers,
    {
        clickout: false, 
        toggle: false,
        multiple: false, 
        hover: false,
        onSelect: function(event) {
            console.log(event.feature);
        },
    }
);

selectControl.activate();

I hope somebody has been solve the problem!

  • This might get better answers on stackoverflow, since it is a Pure JavaScript/jQuery Question. – Devdatta Tengshe Apr 16 '13 at 10:51
  • @DevdattaTengshe Well, I think it's still more OpenLayers question, as I can solve the similar problems in JS in different ways, however I'm totally stuck there. I guess I'm missing something regarding the OpenLayers ... – Mahdi Apr 16 '13 at 10:54
  • I wish OpenLayers guys spend more time for the documentation. I'm pretty sure they will receive more attention (or even more donation), or at least more users!, if they make a better documentation ... – Mahdi Apr 16 '13 at 10:59
  • Where/how are you adding the SelectFeature control to the map? If you're overwriting the control and then only adding the last one you created to the map, only that one will work. You will have to add all the controls to the map. – René Wolferink May 16 '13 at 13:21
  • @RenéWolferink I've already solved the problem, please take a look at my answer below. – Mahdi May 16 '13 at 13:32
0

Here we go! :)

if (typeof selectControl == "object") {
    selectControl.setLayer(map.getLayersByClass("OpenLayers.Layer.Vector"));

} else {

    selectControl = new OpenLayers.Control.SelectFeature(
        layer,
        {
            clickout: false, 
            toggle: false,
            multiple: false, 
            hover: false,
        }
    );

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

layer.events.on({
    "featureselected": function(event) {
        console.log(event.feature);
    }
});

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.