Select to view content in your preferred language

Delete Indisvidual graphics?

3100
12
Jump to solution
02-12-2013 12:59 PM
ionarawilson1
Deactivated User
I have a tool that creates labels (graphics) when the user clicks on a button and then on the map. Is there a way to delete these labels one by one? Thank  you!!!  Here is the code:
 private function addtext():void  {       myMap.addEventListener(MapMouseEvent.MAP_CLICK, onClickFunction);      }       private function onClickFunction(event:MapMouseEvent):void       {            const mapPoint:MapPoint = event.mapPoint;    mapPoint.spatialReference = new SpatialReference(102100);            var myGraphicMarker:Graphic = new Graphic(mapPoint, new TextSymbol(labeltext.text,null,0x000000,1, tsBorder.selected,tsBorderColor.selectedColor,tsBackground.selected,tsColor2.selectedColor,placement.selectedItem,angle.value,xoffset.value,yoffset.value,new TextFormat(myFont.selectedItem, tfSize.value, tsColor1.selectedColor, tfBold.selected,tfItalic.selected, tfUnderline.selected),      null,null));    theTextGraphic = myGraphicMarker        //myGraphicMarker.toolTip = "Marker added with ActionScript";    pointGraphicsLayer.add(theTextGraphic);    pointGraphicsLayer.refresh();      }
Tags (2)
0 Kudos
12 Replies
ionarawilson1
Deactivated User
Thank you Mark.  Where do I call the application1_creationCompleteHandler function? do i call it in the creation complete of the graphiics layer or the application? Thanks
0 Kudos
MarkHoyland
Frequent Contributor
I added in creationComplete of the application,
but it could be with the map's creationComplete.

Here is the full test app. (this was is with 2.5 arc gis flex api. The textSymbol for latest api is slightly different with an alpha parameter. I added a commented textSymbol which should work with newer api, but untested by me.).

<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"        xmlns:s="library://ns.adobe.com/flex/spark"        xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:esri="http://www.esri.com/2008/ags"       creationComplete="application1_creationCompleteHandler(event)">  <fx:Script>   <![CDATA[    import com.esri.ags.Graphic;    import com.esri.ags.events.MapMouseEvent;    import com.esri.ags.geometry.MapPoint;    import com.esri.ags.symbols.TextSymbol;        import mx.events.FlexEvent;            //[Bindable]     private var graphicContextMenu:ContextMenu = new ContextMenu();        protected function application1_creationCompleteHandler(event:FlexEvent):void    {     graphicContextMenu.hideBuiltInItems();     var item:ContextMenuItem;     item = new ContextMenuItem("Delete me");     //item.enabled = true;     item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, deleteGraphic_handler);     graphicContextMenu.customItems.push(item);    }        protected function deleteGraphic_handler(event:ContextMenuEvent):void    {     trace("deleteGraphic_handler");     if (event.contextMenuOwner is Graphic)     {      var graphic:Graphic = event.contextMenuOwner as Graphic;      graphic.graphicsLayer.remove(graphic);     }    }        protected function map_mapClickHandler(event:MapMouseEvent):void    {     const mapPoint:MapPoint = event.mapPoint;          var textSymbol:TextSymbol = new TextSymbol("myTextSymbol",null,0x000000, true,0,true); //2.5 api     //var textSymbol:TextSymbol = new TextSymbol("myTextSymbol",null,0x000000, 1, true,0,true); // newer api               var myGraphicMarker:Graphic = new Graphic(mapPoint, textSymbol);          myGraphicMarker.contextMenu = graphicContextMenu;          graphicsLayer.add(myGraphicMarker);     graphicsLayer.refresh();    }                   ]]>  </fx:Script>  <fx:Declarations>   <!-- Place non-visual elements (e.g., services, value objects) here -->  </fx:Declarations>       <esri:Map id="map" mapClick="map_mapClickHandler(event)" >   <esri:extent>    <esri:Extent xmin="-10753431" ymin="4624151" xmax="-10737799" ymax="4635884">     <esri:SpatialReference wkid="102100"/>    </esri:Extent>   </esri:extent>   <esri:ArcGISTiledMapServiceLayer id="baseLayer" url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>   <esri:GraphicsLayer id="graphicsLayer"/>  </esri:Map>     </s:Application> 
0 Kudos
ionarawilson1
Deactivated User
Thank you so much Mark. It worked. I might create another thread to ask questions about this issue with new code I created because I am curious to what I am doing wrong, but this is also a great solution and I am going to include in my app! Thanks again for your time and interest in posting this!!!
0 Kudos