protected function layer_clickHandler(event:MouseEvent):void { selectedGraphic = Graphic(event.target.parent.parent); editTool.activate(EditTool.MOVE, [selectedGraphic]); // To detect when the graphic is moving selectedGraphic.graphicsLayer.renderer = getRenderer(0x00FF00); // affects a custom renderer to the graphicsLayer. This is called each time a feature is clicked in order to set the symbol of previously clicked features to the original symbol. // ... And then I change the symbol of the clicked graphic: selectedGraphic. }Solved! Go to Solution.
public function getSymbol(graphic:Graphic):Symbol { if (graphic === mySelectedGraphic) { return selectedSymbol; } return otherSymbol; } public function getSymbol(graphic:Graphic):Symbol { if (graphic === mySelectedGraphic) { return selectedSymbol; } return otherSymbol; }<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:esri="http://www.esri.com/2008/ags"
xmlns:s="library://ns.adobe.com/flex/spark"
initialize="initApp()"
pageTitle="Example - using Renderers">
<!--
Based on this sample http://resources.arcgis.com/en/help/flex-api/samples/index.html#/Using_Renderers/01nq00000052000000/
-->
<fx:Script>
<![CDATA[
import com.esri.ags.Graphic;
import com.esri.ags.events.GraphicEvent;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.symbols.Symbol;
private function initApp():void
{
var i:int, mapX:Number, mapY:Number, graphic:Graphic;
// add 100 random graphics to the GraphicsLayer
for (i; i < 100; i++)
{
mapX = Math.random() * 40044000 - 20022000;
mapY = Math.random() * 40044000 - 20022000;
var attributes:Object = { "ranking": Math.random()};
graphic = new Graphic(
new MapPoint(mapX, mapY),
null,
attributes
);
graphic.toolTip = "Ranking: " + Number(graphic.attributes.ranking).toFixed(3);
graphicsLayer.add(graphic);
}
// add 10 random graphics to the GraphicsLayer
for (i = 0; i < 10; i++)
{
mapX = Math.random() * 40044000 - 20022000;
mapY = Math.random() * 40044000 - 20022000;
graphic = new Graphic(
new MapPoint(mapX, mapY),
blueSym
);
graphic.toolTip = "Item without ranking";
graphicsLayer.add(graphic);
}
}
protected function onGraphicAdd(event:GraphicEvent):void
{
var gr:Graphic = event.graphic;
gr.addEventListener(MouseEvent.CLICK, onGraphicClick);
}
private var lastGraphic:Graphic = null;
private var lastSymbol:Symbol = null;
protected function onGraphicClick(event:MouseEvent):void
{
var gr:Graphic = Graphic(event.target);
// remove listener if needed
//gr.removeEventListener(MouseEvent.CLICK, onGraphicClick);
restoreLastGraphicSymbol();
highlightSelectedGraphic(gr);
}
private function restoreLastGraphicSymbol():void
{
if (lastGraphic && lastSymbol)
{
// find last clicked graphic
for each(var gr:Graphic in graphicsLayer.graphicProvider)
{
if (gr.id == lastGraphic.id)
{
// restore symbol
gr.symbol = lastSymbol;
// restore listener if removed
//gr.addEventListener(MouseEvent.CLICK, onGraphicClick);
break;
}
}
}
}
private function highlightSelectedGraphic(graphic:Graphic):void
{
// backup graphic
lastGraphic = graphic;
// backup its symbol
if (graphic.symbol)
{
lastSymbol = graphic.symbol;
}
else
{
lastSymbol = graphicsLayer.renderer.getSymbol(graphic);
}
// set highlight symbol
graphic.symbol = highlightSym;
}
]]>
</fx:Script>
<fx:Declarations>
<esri:SimpleMarkerSymbol id="blueSym"
style="triangle"
alpha="0.7"
color="0x0000FF"
size="14"/>
<esri:SimpleMarkerSymbol id="smallSym"
alpha="0.7"
color="0xFF0000"
size="6"/>
<esri:SimpleMarkerSymbol id="mediumSym"
alpha="0.7"
color="0xFF0000"
size="10"/>
<esri:SimpleMarkerSymbol id="largeSym"
alpha="0.7"
color="0xFF0000"
size="16"/>
<esri:SimpleMarkerSymbol id="highlightSym"
style="diamond"
alpha="0.7"
color="0xFFFF00"
size="16"/>
</fx:Declarations>
<esri:Map>
<esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
<esri:GraphicsLayer id="graphicsLayer" graphicAdd="onGraphicAdd(event)">
<esri:renderer>
<esri:ClassBreaksRenderer field="ranking">
<esri:ClassBreakInfo maxValue="0.33" symbol="{smallSym}"/>
<esri:ClassBreakInfo maxValue="0.67"
minValue="0.33"
symbol="{mediumSym}"/>
<esri:ClassBreakInfo minValue="0.67" symbol="{largeSym}"/>
</esri:ClassBreaksRenderer>
</esri:renderer>
</esri:GraphicsLayer>
</esri:Map>
</s:Application>