*Using code from Genia S. I have appended my 'workaround' to the bottom of the post *
My head is spinning from 3 hours of browsing forums and trying snippets of code. My main problem is that I am not a programmer, I hack together code until I get it to work. Any explanation or direction will be appreciated. Variables, objects, arrays all seem to be the same in some ways, I don't understand the intricacies.
I want to take away the repetitiveness of this code (using leafletjs for constructor). Copy/pasting the same code 20-30 times does not seem elegant:
var manholes = new L.TileLayer.WMS("http://wms.server.address", {
layers: 'manholes',
format: 'image/png8',
transparent: true
});
var stormdrains = new L.TileLayer.WMS("http://wms.server.address", {
layers: 'stormdrains',
format: 'image/png8',
transparent: true
});
continued for each layer I need to create
That seems like a lot of redundant code when I change only one word (manholes, stormdrains, etc....) in only two places. So, I am trying to streamline this process into something like:
var layerlist = ["manholes","stormdrains","gravitylines","pumpstations"];
for (i = 0; i < layerlist.length; i++) {
var layerlist[i] = new L.TileLayer.WMS("http://wms.server.address", {
layers: layerlist[i],
format: 'image/png8',
transparent: true
});
};
Please, do not assume that I know why I am using _____ instead of ____; or why I am doing _____ when I should be doing ________. I began this post by admitting my lack of understanding.
Below is the code I am using. Instead of pushing to a new array as Genia suggested, I am using window[] to create new objects (I have no idea how this works). My problem using Genia's solution was that further in the code, leafletjs was throwing exceptions trying to access the objects within the new array using . notation. If this can be remedied, I am glad to learn. Thank you all for the help, and thanks to Genia for the comments and replies. All bad practices and butchered code are my own doing, and nothing negative should be attributed to Genia.
var layerlist = ["manholes","stormdrains","gravitylines","pumpstations"];
for (var i = 0; i < layerlist.length; i++) {
var name = String(layerlist[i])
window[name] = new L.TileLayer.WMS(ramnode, {
layers: name,
format: 'image/png8',
transparent: true
});
};
now I have four new, individual objects named manholes, stormdrains, gravitylines, pumpstations that act exactly the same as if I had created them the original way (first code example at the top)