How can we set the order of layer based on id. I have tried providing an index while adding the layers but it does not seem to work and also it would be error prone because multiple layers will be getting added through out the app
Assuming you have an array of layer IDs that are sorted in the order you want, and also with the lowest layer at index 0, it seems to me that you could add a function like this and call it as needed (e.g. after adding a layer, etc):
function resetLayerOrder(map, sortedLayerIDs) {
var mapLayerIndex = 0;
sortedLayerIDs.forEach(function(layerID) {
var layer = map.findLayerById(layerID);
if (layer)
map.reorder(layer, mapLayerIndex++);
});
}
(This also assumes you're working with version 4.x of the API; see also Map.reorder.)
This seems to be working. But there is a scenario if there are multiple layers with the same id, then it orders the layer in the sequence they were added. For ex: We have set the ordering as : [graphiclayer, imagelayer] and we have 2 layers with a graphic added on it and 1 layer with an image. Suppose we add a graphic , then an image and then again a graphic. In this case, the last graphic gets added on the top instead of bottom.
Any fix for such scenario ?
The documentation indicates that layers must have unique IDs, so going against that would give unpredictable results. I would recommend adjusting your application so layers have unique IDs.
Thanks for the reply. Could you please confirm if there is any other parameter on the basis of which layer ordering can be done ? Any "order" property or something like that which different layer can share
No, there aren't any layer properties native to the API that support layer ordering. That's not to say you can't simply assign one though. For example:
var layer1 = new MapImageLayer({url:"someURL"});
layer1.myLayerOrder = 1;
var layer2 = new MapImageLayer({url:"someOtherURL"});
layer2.myLayerOrder = 2;
You would just need to ensure you assign that custom property for all the layers in your application.