Hi, I'm doing R&D about the map application that can rearrange order layer.
For example I add 5 layers according to below sample code
var baseMap1 = new ArcGISTiledMapServiceLayer(urlBasemap1, { id: "BASEMAP1" });
var dynamicMap1 = new ArcGISTiledMapServiceLayer(urlDynamicMap1, { id: "DYNAMICMAP1" });
var graphicsLayer1 = new ArcGISTiledMapServiceLayer({ id: "GRAPHIC1" });
var dynamicMap2 = new ArcGISTiledMapServiceLayer(urlDynamicMap2, { id: "DYNAMICMAP2" });
var featuresLayer1 = new ArcGISTiledMapServiceLayer(urlFeaturesLayer1, { id: "FEATURESLAYER1" });
map.addLayer(baseMap1);
map.addLayer(dynamicMap1);
map.addLayer(graphicsLayer1);
map.addLayer(dynamicMap2);
map.addLayer(featuresLayer1);
But "map" object will keep graphics & features layer ID separately from other layers. Therefore, it keeps each layer id as graphicsLayerIds ("GRAPHIC1,FEATURESLAYER1") and layerIds ("BASEMAP1,DYNAMICMAC1,DYNAMICMAP2")
If I would like to get index of layer order as BASEMAP1 --> 0, DYNAMICMAP1 --> 1 , GRAPHIC1 --> 2 , DYNAMICMAP2 --> 3 , FEATURESLAYER1 --> 4, do ArcGIS API Javascript can help me with this or do I have to manually create a config object for these layer order by myself?
Thanks
Solved! Go to Solution.
If you want to get the layer index of layers within the Dynamic map service, Jon's sample works. But, if you want to know the index of layers added to the map, then you would need to maintain a local array of values.
First of all I would like to clarify that the LayerId and index of a layer are 2 different things. The values you get from Map.layerIds and graphicsLayerIds are unique names for the layers added to the map, it does not mean that they are in the same order on the map.
To Identify the index you could use the getLayerVisibleAtScale method to get a list of layers and get the index for the layerId you need. This will work if there is no min/maxScale set for the layers. ie., all layers are visible at all levels. There is also an internal property of map "_layers" which contains the list of all the layers. I would suggest to use this with caution.
Hope this was useful.
Warm Regards,
Thejus
According to the Javascript API, layers are appended to the map in the same order you add them, so your script should give you the order you want. Is that not the case?
As a side note, if you are adding multiple layers, it is better to use addLayers to add them all at once.
Sorry for unclear explanation.
Actually, I want to find the layer index in case that I don't know or remember how many layer I have added to the map.
For example, if there were 5 layers added to the map but I don't know which one is added before or after, I know only that there is one layer has an id "GRAPHIC1", I want to find out that what is the index order for the layer "GRAPHIC1".
You can use something like the getLayerIndex function here. I think I got it from one of the Javascript samples, but can't remember which one, sorry. In this case, I'm adding a GP result map that has many layers and I only want to display one, but I think you could use the same principle to get the index of a layer when you only have its name.
function DrawResultLayer (resultMap, resultUrl, resultName) { require(["esri/layers/ArcGISDynamicMapServiceLayer"], function (ArcGISDynamicMapServiceLayer) { var mapServiceLayer, resultUrl; function getLayerIndex(layer, name) { var infos = layer.layerInfos, layerNames = []; for (var i = 0, il = infos.length; i < il; i++) { layerNames.push(infos.name); } return layerNames.indexOf(name); } mapServiceLayer = new ArcGISDynamicMapServiceLayer(resultUrl); mapServiceLayer.on('load', function (evt) { var layerIndex; // first turn off all layers evt.layer.setVisibleLayers([-1]); layerIndex = getLayerIndex(evt.layer, resultName); if (layerIndex === -1) { console.log("Could not find layer for " + resultName); } else { // then turn on result layer if found evt.layer.setVisibleLayers([layerIndex]); } }); resultMap.addLayer(mapServiceLayer); } }
Apologies, this script editor seems to have killed my indentation. Never mind, hopefully you get the idea.
If you want to get the layer index of layers within the Dynamic map service, Jon's sample works. But, if you want to know the index of layers added to the map, then you would need to maintain a local array of values.
First of all I would like to clarify that the LayerId and index of a layer are 2 different things. The values you get from Map.layerIds and graphicsLayerIds are unique names for the layers added to the map, it does not mean that they are in the same order on the map.
To Identify the index you could use the getLayerVisibleAtScale method to get a list of layers and get the index for the layerId you need. This will work if there is no min/maxScale set for the layers. ie., all layers are visible at all levels. There is also an internal property of map "_layers" which contains the list of all the layers. I would suggest to use this with caution.
Hope this was useful.
Warm Regards,
Thejus
Thanks Thejus! that's what I want
Jon, thank you for your reference here. I was going down a rabbit hole trying to ensure a layer that was added in using the addLayers method:
map.addLayers([layerName1, layerName2, etc...]);
When all that was needed was to ensure the last drawn (top layer) was loaded last in the list.
I am however still making use of the map.reorderLayer(layerName, layerIndexPosition)
function for other layers to sit on top of the "initially" loaded layers, which is really useful.
Ultimately though I wish out-of-the-box the Esri JS API would collect the layer position number somewhere. I did some investigating, but was unable to see where the information resided. IMO, why should the developer have to create additional code to store something that seemingly is maintained in some manner behind the scenes?
Has anyone done investigation to see if this issue is resolved in version 4? In my initial R&D I wasn't able to find a solution.