Select to view content in your preferred language

Change index of a graphic in a GraphicsLayer

9327
14
11-20-2015 12:23 PM
TyroneBiggums
Deactivated User

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.

Tags (2)
0 Kudos
14 Replies
TyroneBiggums
Deactivated User

Is there a magic trick for graphics? Or is remove/add graphics the way to go still. Thanks for the pointer to reorderLayer. That worked great.

0 Kudos
GirishYadav
Deactivated User

There is no in-built method to reorder the graphics, But, as Layer.Graphics is an array, we can manipulate it with javascript array methods.

Sure, you can also use remove/add graphics, but the way I prefer is this:

Array.prototype.reorder = function (fromIndex, toIndex){
     this.splice(toIndex, 0, this.splice(fromIndex, 1)[0]);
}
//an example
var a = [1,2,3,4,5];
a.reorder(3, 0);
console.log(a)
//a is now [4,1,2,3,5]

This is added as an extension method, therefore it can be used with any array in your app. However, if you just want to use it in one place then simply use one line that is inside the function.

-Girish

0 Kudos
TyroneBiggums
Deactivated User

I haven't had a chance to test this out, yet. Do the graphics re-order on the map with this method, or are there further steps? In other words, if the graphics are all on top of each other with 100 opacity, does this bring a graphic to the top?

0 Kudos
BrandonFlessner
Frequent Contributor

the .moveItem() method does not appear to exist at 4.5. I tried using .reorder(graphic, 0). Using .indexOf(graphic) I see that indeed the graphic was moved to the 0 position in the graphics collection, but the graphicsLayer does not redraw. Any ideas?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Brandon,

  It sounds like a bug where the layer does not re-draw based on the reorder. You can force the view to refresh by setting the view.extent = view.extent.