How to get all layers and sublayers of a Map in a flat collection

5617
1
Jump to solution
05-16-2017 11:39 AM
SamuelAbati
New Contributor III

I have a WebMap : WebMap | API Reference | ArcGIS API for JavaScript 4.3 

Layers in a WebMap are MapImageLayers that contain SubLayers.

In the WebMap API Reference, it mentions that WebMap.allLayers it's a flat collection of all layers, but not sublayers.

My question is, how can I have a collection of all "effective" layers and sublayers, and not group layers.

Because how am I supposed to find a layer by title/url that is specified in a configuration file in my map?

This is what I've tried :

        webmap = new WebMap({
          portalItem: { // autocasts as new PortalItem()
            id: "5bc9a1fdb2ea4d359f2404cd51a64605"
          }
        })
        .load()
        .then(function(instance){
            console.log('Map allLayers :', instance.allLayers.toJSON()); //No sublayers
            return new MapView({
                map: instance,
                container: "map"
            })
        })
        .then(function(instance){
            console.log('View.map allLayers :',instance.map.allLayers.toJSON()); //Same thing as above
            console.log('View allLayerViews :',instance.allLayerViews.toJSON()); //Empty
        });

0 Kudos
1 Solution

Accepted Solutions
ThomasSolow
Occasional Contributor III

Try Collection.flatten: Collection | API Reference | ArcGIS API for JavaScript 4.3 

There's a sample there that looks like it may be just what you're looking for.

View solution in original post

1 Reply
ThomasSolow
Occasional Contributor III

Try Collection.flatten: Collection | API Reference | ArcGIS API for JavaScript 4.3 

There's a sample there that looks like it may be just what you're looking for.