kaycee
kaycee

Reputation: 911

Reach layer name property from a layer group in OpenLayers3

I have a layer group consisting of 3 layers and named for example: group1.

I want to produce an array of the names of the layers that are in this group. I'm actually doing that:

group1.getLayers().getArray()

I'm reaching the level of the ol.layer.vector, but I can't seem to find how to access the value of the name propertie.

My guess was to do add a .get("name") after the getArray() but it doesn't work (I guess you can't call more than 2 methods). I also tried stocking the results of the getArray() in a variable, but then I can't do a get("name") on the variable.

How can I access my layers name ?

Edit: Looks like I can use the getLayersArray() method also, but I'm stuck with the same problem.

Upvotes: 2

Views: 971

Answers (1)

kaycee
kaycee

Reputation: 911

Ok I found my answer.

function getNames(dom){
    var stock = [];
    var layer_names = [];
    stock = dom.getLayersArray();

    for(i=0;i<stock.length;i++){
        layer_names[i] = stock[i].get("name");
    };
    return layer_names;
}

Thanks to @1saac .

Edit: Pushed it a little to add properties keys of each layer:

function getNames(domaine){
        var r_stock = [];
        var r_layers = [];
        r_stock = domaine.getLayersArray();

        for(i=0;i<r_stock.length;i++){

            feat_source = r_stock[i].getSource();
            feat_get = feat_source.getFeatures();
            prop_feature[i] = feat_get[0].getKeys();

            r_layers[i] = ["name", r_stock[i].get("name")];

            for(j=0;j<prop_feature[i].length;j++){
                r_layers[i].push("id" + j, prop_feature[i][j])
            };

        };

        return r_layers;
    };

Upvotes: 2

Related Questions