How do I add multiple layers to the LayerList?

2050
8
Jump to solution
08-24-2017 07:26 AM
GregSalvador
New Contributor III

Hi all,

I would like to add multiple layers to the LayerList dijit (using 3.21). I tried to add each layer to an array as it was added to the map and then reference the array when making the LayerList, but this left out all of the sublayers. here is my code:

var layerArray = new Array();
var tempLayer = new ArcGISDynamicMapServiceLayer(
"https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapSe...");
layerArray.push(map.addLayer(tempLayer));

.....repeated with different layers.......

//layerlist widget initialization
var layerListWidget = new LayerList({
map: map,
layers: layerArray
}, "layerList");
layerListWidget.startup();

Is there a way to add all of the layers (and sublayers) to the layerList Dijit programmatically?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Greg,

  You will see in the widget documentation that if you do not add a layers property in the LayerList widget constructor then it will display all layers that are in the map. You do need to wait for the layers to be added to the map before creating the LayerList widget though.

View solution in original post

8 Replies
RobertScheitlin__GISP
MVP Emeritus

Greg,

  You will see in the widget documentation that if you do not add a layers property in the LayerList widget constructor then it will display all layers that are in the map. You do need to wait for the layers to be added to the map before creating the LayerList widget though.

GregSalvador
New Contributor III

Thank you Robert. I don't know how I missed this tidbit, but thanks

0 Kudos
GregSalvador
New Contributor III

So I have another question: how do i remove a layer from the layerList? I noticed that one of the layers added is the basemap, and I don't want users to be able to toggle it on and off. I didnt see a remove function, so do I have to instead define all of the layers that I want in the list and omit the basemap Layer?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Do I have to instead define all of the layers that I want in the list and omit the basemap Layer?

Correct

Our you could wait for the load event on the widget and manipulate the layers array.

0 Kudos
GregSalvador
New Contributor III

Hi again.
I am trying to wait for the load event on the widget and the manipulate the layers array. I seem to be doing something wrong, as i get a browser error saying "TypeError: c is null". The problem seems to be with my on("load") statement, but all of the examples I have seen seem to do it similarly. I checked, and my require "dojo/on" and the function order is the same, and I've called the on("load") function after I have declared the LayerList. Is there a step i am missing somewhere? Here is my code:
require([
"esri/map",
"esri/arcgis/utils",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/dijit/LayerList",
"esri/dijit/BasemapGallery",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/parser",
"dojo/on",
"dojo/domReady!"
], function (Map, arcgisUtils, ArcGISDynamicMapServiceLayer, LayerList, BasemapGallery, parser, on) {
...declaring layers and map object.....

//layerlist widget initialization
var layerListWidget = new LayerList({
map: map,
}, "layerList");
layerListWidget.startup();
on(layerListWidget, "load", function (item) {
item.layers.reverse();
item.layers.pop();
item.layers.reverse();
});

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,

   Your requires and corresponding vars are out of order.

You have:

require([
"esri/map",
"esri/arcgis/utils",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/dijit/LayerList",
"esri/dijit/BasemapGallery",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/parser",
"dojo/on",
"dojo/domReady!"
], function (Map, arcgisUtils, ArcGISDynamicMapServiceLayer, LayerList, BasemapGallery, parser, on) {

Should be:

require([
"esri/map",
"esri/arcgis/utils",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/dijit/LayerList",
"esri/dijit/BasemapGallery",
"dojo/parser",
"dojo/on",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/domReady!"
], function (Map, arcgisUtils, ArcGISDynamicMapServiceLayer, LayerList, BasemapGallery, parser, on) {
GregSalvador
New Contributor III

ok, I get it, i think. All of the requires with a corresponding variable have to be first, followed the the requires that don't have a variable? do the requires with a variable need to be in any specific order(as long as they stay synced up)? 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

As long as they stay synced up (listed in the same order) then you are fine.

0 Kudos