Select to view content in your preferred language

Writing Custom Legend - Get order of layers in map

1027
3
Jump to solution
09-29-2014 08:57 PM
JustinCornell
New Contributor III

I'm writing a custom legend, one that looks and functions just like the silverlight legend.  I have everything working except the order of the layers.  How can I get the order of the layers in the map?

From what I can figure out, there are two properties on the Map that give the layers in the map.  Those properties are Map.layerIds and Map.graphicsLayerIds but there is not a single property that gives the order of the map layers.  I don't know how to tell if a graphics layer was added before or after a given non-graphic layer.  I know I can add a Map.on('layer-add', function(args){..}); but I am hoping not to do that as I cannot guarantee the map is assigned to the legend before layers are added. How can I get the order of the map layers?

Any help would be great!

0 Kudos
1 Solution

Accepted Solutions
Vara_PrasadM_S
Occasional Contributor II

Hi Justin Cornell,

In JS API, below is the order of the type of layers added/rendered on map.

1. Graphic Layers (Top)

2. Dynamic Map Services

3. Tiled Map Services (Bottom)

We can re order a Graphic Layer only among the graphic layers added to the map.

Thanks & Regards,

Vara Prasad.

View solution in original post

3 Replies
Vara_PrasadM_S
Occasional Contributor II

Hi Justin Cornell,

In JS API, below is the order of the type of layers added/rendered on map.

1. Graphic Layers (Top)

2. Dynamic Map Services

3. Tiled Map Services (Bottom)

We can re order a Graphic Layer only among the graphic layers added to the map.

Thanks & Regards,

Vara Prasad.

JustinCornell
New Contributor III

Thanks you for that explanation!  I guess since tiled layers can be added outside of specifying a base layer.  Should I assume they are always rendering on the bottom even if they show up in the middle of the layerIds array, or are you saying all base layers will be rendered on thr bottom and then the layerIds would be rendered in the order they appear?

Supposing the layerIds like this...

['FeatureLayer1', 'TiledLayer1', 'DynamicLayer1', 'TiledLayer2']

Would the render order be this?

[ 'TiledLayer1', 'TiledLayer2', 'FeatureLayer1', 'DynamicLayer1', 'TiledLayer2']

If not what would the render order be?

0 Kudos
JustinCornell
New Contributor III

This is the code I came up with... will this code do what I want?

    function _getLayerLegendIndexOrder(layer) {

        if (layer instanceof esri.layers.GraphicsLayer) {

            return _map.graphicsLayerIds.indexOf(layer.id) + _map.layerIds.length;

        }

        return _map.layerIds.indexOf(layer.id);

    }

0 Kudos