How to find layer group from layer in Openlayers 3?
Asked Answered
I

2

5

I'd like to be able to tell what ol.layer.Group(s) a layer is part of during a user interaction, without going through all the groups on the map top-down.

Is there a way to do this? I'm currently using ol3 v3.10.2.

Immix answered 21/10, 2015 at 18:4 Comment(0)
L
3

Looking at the ol.layer.Group, ol.collection and goog.array, none of these set an backwards reference to the layer. So you'll have to dig down trough all the groups, as far as I can see.

Laveta answered 23/10, 2015 at 7:15 Comment(0)
C
6

During initialising of your layers asign an attribute, on each layer, to verify the group this layer belongs to. like so:

var vector = new ol.layer.Vector({
  GROUP : 'group1',
  source: vectorSource,
  style: new ol.style.Style({
        image: new ol.style.Circle({
            radius: 5,
            fill: new ol.style.Fill({color: '#FFFFFF'}),
            stroke: new ol.style.Stroke({
                color: '#000000',
                width: 3
            })
        })
    })
});

and then you may get the group like so:

layer.get('GROUP');
Craw answered 23/10, 2015 at 12:14 Comment(1)
This works. So does keeping a separate dictionary of all the group and layer 'id's. This isn't in the library though, that's why I have not marked it as the answer.Immix
L
3

Looking at the ol.layer.Group, ol.collection and goog.array, none of these set an backwards reference to the layer. So you'll have to dig down trough all the groups, as far as I can see.

Laveta answered 23/10, 2015 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.