1

I wanna do something like this ;

Openlayers 3 Vector Layer Example

But I don't want to one layer, 3 or more layer on the map and get feature like the example.

How can I separate the layer programmatically ?

    if (feature) {
    info.innerHTML = feature.getId() + ': ' + feature.get('name');
  } else {
    info.innerHTML = ' ';
  }

Because I will need to change the feature.get('name') properties each layer.

2 Answers 2

1

If I understand your question, you want to be able to change a property on all or some of the features (on separate layers) on the map.

From the ol.map object, you can get the layergroup property. This will give you access to all the layers on the map, you can then just do something like:

var layers = map.get('layergroup').getLayersArray();
for (var i in layers) {
  // The features are inside a source object
  var features = layers[i].getSource().getFeatures();
  for (var f in features) {
    // Set properties based on whatever logic 
    features[f].set('foo', 'bar');
  }
}
2
  • Thanks for the answer, but I get this error message : TypeError: map.get(...).getLayersArray is not a function
    – serifsadi
    Oct 17, 2014 at 7:19
  • Sorry about that, it is odd though - that is part of the API: github.com/openlayers/ol3/blob/master/src/ol/layer/… but if you do map.get('layergroup').getLayers() it should give you an ol.Collection which you can get an array from if you'd like.
    – Timh
    Oct 18, 2014 at 17:43
0

The basic trick is loop through the features and concatenate result to the innerHTML.

var info = document.getElementById('info');
info.innerHTML = ''; // clears out the current content
var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
  info.innerHTML += feature.getId() + ': ' + feature.get('name') + '<br/>';
});
2
  • OK, I understand, but i had to change the property, because each layer has a different element. For example, One of them has 'name', one of them 'area'. I think, if conditions maybe help. But i didn't catch the layer.
    – serifsadi
    Oct 16, 2014 at 6:37
  • you got it. Just look at the example in the answer: forEachFeatureAtPixel is called with with the feature and the layer as argument.
    – tonio
    Oct 17, 2014 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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