Select to view content in your preferred language

How to change the order of layers ?

5610
3
Jump to solution
04-03-2013 08:13 AM
JeremieJoalland1
Deactivated User
I want to change the order of my Layers programmatically (using ArcGIS Runtime Java 10.1.1)

in API Reference there LayerList.set(int index, Layer element) to set the given layer at the given index, but when I'm trying to do same, I'm getting the folowing error :

java.lang.IllegalArgumentException: Layer is already in another map
at com.esri.map.LayerList.a(Unknown Source)
at com.esri.map.LayerList.set(Unknown Source)
at com.esri.client.samples.mapping.graphics.AddGraphics.changedOrderOfLayer(AddGraphics.java:110)

I tried with 3 cases:
- 1 ArcGISLocalTiledLayer and 1 GraphicsLayer
- 2 ArcGISLocalTiledLayer (another point here : LayerList.add() return "true" for 2nd Layer, but I can only see one TPK on my map...)
- 1 ArcGISLocalTiledLayer and 1 ArcGISLocalDynamicMapServiceLayer

my simple code :
private void changedOrderOfLayer() {
 System.out.println("Before - layer's name at index 1 is : " + map.getLayers().get(1).getName());
 
 // Move Layer to index 0
 map.getLayers().set(0, map.getLayers().get(1)); // IllegalArgumentException HERE !!!
 
 System.out.println("after - layer's name at index 1 : " + map.getLayers().get(0).getName());
}


What is the best way to change order of Layers in JMap ?
Are there some limitations depending of layers format (TPK, MPK, GraphicsLayer, etc.) ?
0 Kudos
1 Solution

Accepted Solutions
MarkBaird
Esri Regular Contributor
We've just been working on an API improvement for the next release which will do exactly what you want.

It will work something like map.getLayers().reorderLayer(oldIndex, newIndex)

For the moment ay 10.1.1 your best option is to remove the layer you want to reorder from the map control and then add it again at the place you want it; although it's not going to be as efficient as the new method above.

Hope that helps

Mark

View solution in original post

0 Kudos
3 Replies
MarkBaird
Esri Regular Contributor
We've just been working on an API improvement for the next release which will do exactly what you want.

It will work something like map.getLayers().reorderLayer(oldIndex, newIndex)

For the moment ay 10.1.1 your best option is to remove the layer you want to reorder from the map control and then add it again at the place you want it; although it's not going to be as efficient as the new method above.

Hope that helps

Mark
0 Kudos
JeremieJoalland1
Deactivated User
thanks for the explanation.
I've implemented the alternative solution, by removing and adding a layer at expected order index seems to work fine, with acceptable performances !

here is my sample code :

public void setOrderOfLayer(Layer layer, int order) {
 // Process the reordering of layer by removing it, then adding it at the expecting order index
 jMap.getLayers().remove(layer);
 jMap.getLayers().add(order,  layer);
}


this was succefull with following jMap content :
- 1 ArcGISLocalTiledLayer
- 1 ArcGISLocalDynamicMapServiceLayer
- 1 GraphicsLayer
- 1 ArcGISDynamicMapServiceLayer (with Local Server, blanck MPK, to load a Shapefile)
0 Kudos
CarlosColón-Maldonado
Frequent Contributor
The blog Coming up in the Next Release of the ArcGIS Runtime SDK for Java? A lot makes mention of a way of doing this.

Re-ordering graphics layers in a map
It was too bad that this wasn�??t functional and clearer in the first release, but the team nailed it down. You can add *n* number of graphics layers to your map and reorder them on the fly. By the way, you can also use �??send to back�?� and �??bring to front�?� on graphic elements in the same layer. It can work in code as simple as this (Java) (No offense to my WPF friends!):
GraphicsLayer gLayer1 = (GraphicsLayer)map.getLayers().get(1);
map.getLayers().remove(2);
map.getLayers().add(3,gLayer1);



I'm sure it works with any type of map layer.
0 Kudos