bailey
bailey

Reputation: 363

Dynamic Layer Name - Leaflet Layer Control

I would like to have the name of a layer used by Leaflets layer control feature come from a variable instead of a string and based on data from the layer.

The example shown below uses the variable layerName. Is this even possible since it is expecting a name value pair? Is there a work around?

var layerName = feature.properties.condition[0];

//layer control
var baseMaps = {
    "OpenStreetMap": OSM,
    "Aerial Imagery": MapQuestOpen_Aerial
};

var overlayMaps = {
    layerName: layer1,

};

L.control.layers(baseMaps, overlayMaps).addTo(map);

Upvotes: 1

Views: 1711

Answers (1)

muzaffar
muzaffar

Reputation: 1736

Yes, of course you can use the layer name from a variable based on layer data.

Here is the way you should adopt

var overlayMaps = {};// you need to create it empty at first

Then you can add layers to it like below

overlayMaps[layerName] = layer1; //layerName here is the variable whose value you want to use as layer name

Upvotes: 6

Related Questions