1

Heres my code to add an SelectControl with Popup for an existing layer add startup

    map.addLayer(window.markerLayer);

window.markerLayer.events.on({

});

// Popup für Marker
window.selectControl = new OpenLayers.Control.SelectFeature([markerLayer]);
map.addControl(window.selectControl);

window.selectControl.activate();
    window.markerLayer.events.on({
    'featureselected' : onFeatureSelect,
    'featureunselected' : onFeatureUnselect
});

I skipping the code of "onFeatureSelect()" and "onFeatureUnselect()" ...

This is working very well.

Now i want to add additional new layers (OpenLayers.Marker as content) to show on the map (also working well) with the same functionality (showing popups...).

If I copy the code above and apply on the new layers the app is not working anymore...

Next try:

Adding the new Layer with setLayer() Method:

window.selectControl.setLayer(selectControl.layers.concat(newLayer));

But no success...

Thanks !

  • Julien's answer seems to be correct. Just one comment - if you are creating all your layers at once, you can skip setLayer and use window.selectControl = new OpenLayers.Control.SelectFeature([markerLayer, newLayer]); – user1702401 Oct 11 '13 at 20:17
3

You are doing the right thing, but concat() doesn't return you what you think.

The correct way of doing this is:

window.selectControl.setLayer([layer1,layer2]);

If you want to use the layers in your selectFeature variable, then make sure you use the right one. If you have only one layer, the selectControl.layers will be null and you have to use selectControl.layer instead. So your code should look like this (untested):

if(selectControl.layers) {
    window.selectControl.setLayer(selectControl.layers.concat(newLayer));
} else {
    window.selectControl.setLayer([selectControl.layer, newLayer]);
}

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.