Skip to content

Instantly share code, notes, and snippets.

@odoe

odoe/MapComp.as

Created Jan 27, 2011
Embed
What would you like to do?
Sample component of using NativeMappedSignal with ESRI DrawTool and Graphics
package net.odoe.nmsExample.components
{
import com.esri.ags.Graphic;
import com.esri.ags.Map;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.layers.GraphicsLayer;
import com.esri.ags.tools.DrawTool;
import flash.events.Event;
import flash.events.MouseEvent;
import org.osflash.signals.natives.NativeMappedSignal;
import spark.components.Button;
import spark.components.supportClasses.SkinnableComponent;
[Event(name="mapChange",type="flash.events.Event")]
public class MapComp extends SkinnableComponent
{
public static const MAP_CHANGE_EVENT:String = "mapChange";
public function MapComp()
{
super();
}
[SkinPart(required="true")]
public var btnLine:Button;
[SkinPart(required="true")]
public var btnPoly:Button;
private var _map:Map;
[Bindable(event="mapChange")]
public function get map():Map
{
return _map;
}
public function set map(value:Map):void
{
if (_map != value)
{
_map = value;
glayer = new GraphicsLayer();
_map.addLayer(glayer);
drawTool = new DrawTool(map);
// This is nice method of pulling data from the event
// and transforming it into something the rest
// of your application actually cares about
drawEnded = new NativeMappedSignal(drawTool, DrawEvent.DRAW_END, DrawEvent, Graphic).mapTo(
function(event:DrawEvent):Graphic
{
return event.graphic;
});
drawEnded.add(onEndDrawing);
dispatchEvent(new Event(MAP_CHANGE_EVENT));
}
}
protected var drawEnded:NativeMappedSignal;
protected var drawLineStarted:NativeMappedSignal;
protected var drawPolyStarted:NativeMappedSignal;
protected var drawTool:DrawTool;
protected var glayer:GraphicsLayer;
protected function onEndDrawing(graphic:Graphic):void
{
glayer.add(graphic);
}
protected function onStartDrawing(drawType:String):void
{
drawTool.deactivate();
drawTool.activate(drawType);
}
override protected function partAdded(partName:String, instance:Object):void
{
super.partAdded(partName,instance);
if (instance == btnPoly)
{
drawPolyStarted = new NativeMappedSignal(btnPoly, MouseEvent.CLICK, MouseEvent, String).mapTo(DrawTool.EXTENT);
drawPolyStarted.add(onStartDrawing);
}
else if (instance == btnLine)
{
drawLineStarted = new NativeMappedSignal(btnLine, MouseEvent.CLICK, MouseEvent, String).mapTo(DrawTool.POLYLINE);
drawLineStarted.add(onStartDrawing);
}
}
override protected function partRemoved(partName:String, instance:Object):void
{
super.partRemoved(partName,instance);
if (instance == btnPoly)
{
drawPolyStarted.removeAll();
drawPolyStarted = null;
}
else if (instance == btnLine)
{
drawLineStarted.removeAll();
drawLineStarted = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment