Get layer order within map

3564
5
Jump to solution
12-12-2014 08:20 AM
SimonHodge
New Contributor III

Hi,

Does anyone know if there is a way of getting a layers current index / order in the map?  The map has a reorder function where you can pass a layer and an index, but in order to know where I want to place the layer, I need to know the index of the layer I'm moving it above or below.

It occurred to me it might be possible to keep track of the order all layers are added to the map, for example by listing for layer-add type events.  I'm not sure how reliable that would be though and hoped there was a built in mechanism.

Note: I'm not wanting to re-order sub layers within a map service, as per the reorder example here: Reorder layers in map service | ArcGIS API for JavaScript   I want to actually re-order the map service layer itself, relative to other map service layers loaded in the map.

Thanks

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This shouldn't be an issue. A graphics layer will always be placed above dynamic or tiled layers.

View solution in original post

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor

You can use "map.layerIds" to get the listing of layers in your map. This JSBin show that, also showing the basemap layers that are included. These layers will change if you switch basemaps.

0 Kudos
SimonHodge
New Contributor III

Hi Ken,

Thanks for that - I had just come to a similar conclusion: that the layerIds are (I think) returned in their index order (or rather reverse order).  i.e. it will be the order the layers were added to the map by default, but the top most layer for repositioning is 0.  If a layer has been re-ordered then it will appear in a different index in the layerIds array.

However, there is still something I'm not certain of.  There is also a graphicslayerIds property which returns the ids of all the graphics layers and feature layers: these layers aren't included in the layerIds array.  So say you had 5 dynamic map services and 5 feature layers on a map:

layerIds returns [dynamicA, dynamicB, dynamicC, dynamicD, dynamicE]

graphicsLayerIds returns [featureA, featureB, featureC, featureD, featureE]

How would you know what index to set dynamicC to to move it above dynamic?

0 Kudos
KenBuja
MVP Esteemed Contributor

This shouldn't be an issue. A graphics layer will always be placed above dynamic or tiled layers.

0 Kudos
SimonHodge
New Contributor III

Ah, I wondered if that might be the case.  I'll experiment with this further next week but I think that hopefully answers it - I'll let you know!

Thanks for your help.

0 Kudos
SimonHodge
New Contributor III

Thanks for your help - I've got it working.  To correct something I said earlier, the bottom most layer is 0, not the top most.  This makes the maths easier as you pointed out: the graphics and feature layers are always on top so don't skew the ids at all.  It's simply a matter of getting the index in the array of the layer you want to move and adding / subtracting.  It's therefore really easy to create up / down buttons that adjust the position of the selected layer.

Thanks for your help!

0 Kudos