I have used the draw toolbar to create a collection of graphics on added those graphics to a graphicsLayer. Is there a way to change the order, or index, of the graphics within the layer once the graphics have been added?
For example, can I draw a rectangle, circle and triangle (in that order) and change map.getLayer('myLayer').graphics to reorder these graphics?
Is there an Esri way, or will I need to redraw all of the shapes in the order that I would like them?
Thanks.
Tyrone,
At least back in 2011, there wasn't a clean way to do this:
The Esri way then was to remove and re-add.
The setzindex of a graphic in a layer is what I'd be interested in but it looks like it's been removed in the latest version of the API (at least I don't see it anywhere in the API reference guide).
I did see another old post that used getDojoShape().moveToFront(). I suppose I could iterate through all the graphics and move them to front/back in the right order to reorder them. But, moveToFront only makes it look like that shape is the top graphic. The order of the graphics are still the same.
It's looking like remove all and add in the order that I want them in. I just don't want to see graphics flashing. Maybe it's going to be fast enough that the user doesn't see them redrawn?
Interestingly, it's in the Silverlight API:
It's clear in the documentation it's really adjusting the z-index.
Yea, we're going from SL to rewriting in JS. We had that ability in SL.
You can move items to any index in the 4.0 version of the API using the Collection API - Collection | API Reference | ArcGIS API for JavaScript
Since GraphicsLayer.graphics is a Collection, you can use the moveItem() method to do this.
//Gets the item in the 0 index
var graphic = layer.graphics.getItemAt(0);
//moves it to the 5 index
layer.graphics.moveItem(graphic, 5);
Would it also be best to wait on 4.0 for re-ordering the layers themselves? If I add LayerA and LayerB (in that order), LayerB will display on top of LayerA. If I want LayerA shown on top, in 3.x, is that also a remove then re-add as mentioned above for graphics?
Layers are treated the same way as graphics in 4.0 since they are also part of a collection in the map. Map | API Reference | ArcGIS API for JavaScript
So you can use the same moveItem() method to reorder:
//moves LayerA into index 1 (on top of LayerB)
map.layers.moveItem(LayerA, 1);
What about 3.x?
There is a method in Map class to change the layer order.
Map | API Reference | ArcGIS API for JavaScript
-Girish