Search code examples
openlayers-3

Openlayers 3 - add variable to ol.layer.Vector in a second time


in my project I define many var, including this one in example:

var lyr_Confini = new ol.layer.Vector({
                declutter: true,
                source: jsonSource_Confini, 
                style: style_Confini,
                interactive: true,
                title: 'Confini'
            });

In a second time, or in another file called later in the index.html, I would like to add a variable in this ol.layer.Vector without overwriting the other variables like:

permalink: 'example',

to have this example in the end:

var lyr_Confini = new ol.layer.Vector({
                    declutter: true,
                    source: jsonSource_Confini, 
                    style: style_Confini,
                    interactive: true,
                    title: 'Confini',
                    permalink: 'example',
                });

How can I do? Thanks


Solution

  • I managed to solve it on my own. The answer was actually very simple and it was enough to read the Openlayers API well.

    The documentation specifies that it is possible to add properties with setProperties and define a value of a property with set(key,value) therefore the example in question can be solved by adding this code in the same file or in another file called later from the index.html

    lyr_Confini.setProperties('permalink');
    lyr_Confini.set('permalink' , 'example');
    

    you will have finally

    var lyr_Confini = new ol.layer.Vector({
                        declutter: true,
                        source: jsonSource_Confini, 
                        style: style_Confini,
                        interactive: true,
                        title: 'Confini',
                        permalink: 'example',
                    });