Select to view content in your preferred language

Can't add the same Graphic twice to a GraphicsLayer

2176
4
06-06-2011 01:51 AM
yulu
by
Emerging Contributor
Hi all
I met an error when I try to add a Graphic to a GraphicsLayer which have already contained this Graphic.
I don't remember whether is ok by using old version API.
My environments are Flex sdk 4.5, ArcGIS API for Flex 2.3.1 and Windows7 X64.
Thanks for your help !

My code is very simple like below :

private var g:Graphic=new Graphic();
private function addGraphic ():void
{
    graphicsLayer.add(g);
}


And when I call this 'addGraphic ()' function twice,it would rise an error as follow:
RangeError: Error #2006: The supplied index is out of bounds.
 at flash.display::DisplayObjectContainer/addChildAt()
 at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::$addChildAt()
 at mx.core::UIComponent/addChildAt()
 at com.esri.ags.layers::GraphicsLayer/collectionAddHandler()
 at com.esri.ags.layers::GraphicsLayer/collectionChangeHandler()
 at flash.events::EventDispatcher/dispatchEventFunction()
 at flash.events::EventDispatcher/dispatchEvent()
 at mx.collections::ListCollectionView/dispatchEvent()
 at mx.collections::ListCollectionView/addItemsToView()
 at mx.collections::ListCollectionView/listChangeHandler()
 at flash.events::EventDispatcher/dispatchEventFunction()
 at flash.events::EventDispatcher/dispatchEvent()
 at mx.collections::ArrayList/internalDispatchEvent()
 at mx.collections::ArrayList/addItemAt()
 at mx.collections::ListCollectionView/addItemAt()
 at mx.collections::ListCollectionView/addItem()
 at com.esri.ags.layers::GraphicsLayer/add()
 at gtest/button1_clickHandler()
 at gtest/___gtest_Button1_click()
Tags (2)
0 Kudos
4 Replies
IvanBespalov
Frequent Contributor
http://help.arcgis.com/en/webapi/flex/samples/index.html

Here are 11 samples, the 1-st of them "Adding Graphics" - i shows how to add graphics.
0 Kudos
yulu
by
Emerging Contributor
http://help.arcgis.com/en/webapi/flex/samples/index.html

Here are 11 samples, the 1-st of them "Adding Graphics" - i shows how to add graphics.


Thanks for your reply. But my problem is not understanding the method about adding Graphics. Actually, I can add a Graphic to a GraphicsLayer ,but when I plan to add this Graphic that has added before ,it causes the error.
0 Kudos
IvanBespalov
Frequent Contributor
Yes, actually, you can not add a the same Graphic to a GraphicsLayer twice.

Can you tell, what is the base idea? Why do you need in 2 absolutely equals graphics in 1 layer?

<fx:Script>
    <![CDATA[
        import com.esri.ags.Graphic;
        import com.esri.ags.events.MapMouseEvent;
        import com.esri.ags.geometry.Geometry;
        import com.esri.ags.geometry.MapPoint;
   
        import mx.utils.StringUtil;
   
        /**
         * Listen map mouse down handler
         */
        protected function myMap_mapMouseDownHandler(event:MapMouseEvent):void
        {
            var grGeometry:MapPoint = event.mapPoint;
            var grAttributes:Object = new Object();
            grAttributes.creationDate = new Date().toString();
    
            var gr1:Graphic = new Graphic(grGeometry, sms, grAttributes);
            gr1.toolTip = gr1.attributes.creationDate;
            var gr2:Graphic = new Graphic(grGeometry, sms, grAttributes);
            gr2.toolTip = gr2.attributes.creationDate;
    
            var grId1:String = myGraphicsLayer.add(gr1);    
            var grId2:String = myGraphicsLayer.add(gr2);
    
            trace(StringUtil.substitute("Graphic with id: {0} added.", grId1));
            trace(StringUtil.substitute("Graphic with id: {0} added.", grId2));
        }
    ]]>
</fx:Script>
0 Kudos
yulu
by
Emerging Contributor
Yes, actually, you can not add a the same Graphic to a GraphicsLayer twice.

Can you tell, what is the base idea? Why do you need in 2 absolutely equals graphics in 1 layer?

<fx:Script>
    <![CDATA[
        import com.esri.ags.Graphic;
        import com.esri.ags.events.MapMouseEvent;
        import com.esri.ags.geometry.Geometry;
        import com.esri.ags.geometry.MapPoint;
   
        import mx.utils.StringUtil;
   
        /**
         * Listen map mouse down handler
         */
        protected function myMap_mapMouseDownHandler(event:MapMouseEvent):void
        {
            var grGeometry:MapPoint = event.mapPoint;
            var grAttributes:Object = new Object();
            grAttributes.creationDate = new Date().toString();
    
            var gr1:Graphic = new Graphic(grGeometry, sms, grAttributes);
            gr1.toolTip = gr1.attributes.creationDate;
            var gr2:Graphic = new Graphic(grGeometry, sms, grAttributes);
            gr2.toolTip = gr2.attributes.creationDate;
    
            var grId1:String = myGraphicsLayer.add(gr1);    
            var grId2:String = myGraphicsLayer.add(gr2);
    
            trace(StringUtil.substitute("Graphic with id: {0} added.", grId1));
            trace(StringUtil.substitute("Graphic with id: {0} added.", grId2));
        }
    ]]>
</fx:Script>


Yes ,I needn't. I just want to make sure this error be caused by bug or by special design. In my program, people can be free to add the same graphics to the same layer many times. So I have added some code to check whether the graphics had already in before adding.
Your supports are greatly appreciated !
0 Kudos