<?xml version="1.0" encoding="utf-8"?>
<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:viewer="com.esri.viewer.*"
xmlns:esri="http://www.esri.com/2008/ags"
layout="absolute"
creationComplete="init()"
width="414"
height="214">
<fx:Script source="script/Pathpoint.as"/>
<fx:Declarations>
<esri:PictureMarkerSymbol id="picStation3"
source="assets/images/widget/vehicle/stop.png"/>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:ColorPicker id="cpLine" selectedColor="{defaultDrawColor}" change="cpLine_changeHandler(event)"
x="134" y="93" width="31" height="31"/>
</viewer:BaseWidget>
import com.adobe.serialization.json.JSONDecoder;
import com.esri.ags.Graphic;
import com.esri.ags.Map;
import com.esri.ags.SpatialReference;
import com.esri.ags.events.*;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polyline;
import com.esri.ags.layers.GraphicsLayer;
import com.esri.ags.symbols.PictureMarkerSymbol;
import com.esri.ags.symbols.SimpleLineSymbol;
import com.esri.ags.utils.WebMercatorUtil;
import flash.events.MouseEvent;
import flash.utils.*;
import mx.collections.*;
import mx.controls.*;
import mx.core.UIComponent;
import mx.effects.Glow;
import mx.events.*;
import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import spark.events.*;
import spark.primitives.Line;
private static var _instance:Pathpoint;
private var _map:Map;
[Bindable]
private var _mapChanged:Boolean=false;
[Bindable]
private var defaultDrawColor:uint;
private var pArray:Array=new Array;
private var line:Polyline=new Polyline;
private var candraw:Boolean=true;
private var panoGraphic:Graphic;
private var pointLayer:GraphicsLayer;
private var lineLayer:GraphicsLayer;
private var currentLineGraphic:Graphic;
//--------------------------------------------------------------------------
// map
//--------------------------------------------------------------------------
public static function getInstance():Pathpoint
{
if (_instance == null)
_instance=new Pathpoint();
return _instance;
}
private function init():void
{
getInstance();
widgetInit();
}
/**
* 窗体初始化函数
*/
private function widgetInit():void
{
defaultDrawColor=0x3FAFDC;
lineLayer=new GraphicsLayer();
pointLayer=new GraphicsLayer();
this.map.addLayer(lineLayer);
this.map.addLayer(pointLayer);
this.map.addEventListener(MapMouseEvent.MAP_CLICK, test_clickHandler);
}
protected function drag(event:DragEvent):void
{
this.map.addEventListener(MapMouseEvent.MAP_CLICK, test_clickHandler);
}
protected function clear(event:MouseEvent):void
{
pArray.length=0;
lineLayer.clear();
pointLayer.clear();
}
protected function test_clickHandler(event:MapMouseEvent):void
{
var mouseMapPoint:MapPoint=map.toMapFromStage(event.stageX, event.stageY);
map.infoWindow.label="当前位置 共:" + (pArray.length + 1) + "个点";
map.infoWindow.show(mouseMapPoint);
var pointGraphic:Graphic=new Graphic(mouseMapPoint, picStation3);
pointGraphic.toolTip=mouseMapPoint.x + "," + mouseMapPoint.y;
pointGraphic.name="point";
pArray.push(mouseMapPoint);
line.addPath(pArray);
pointLayer.add(pointGraphic);
//0x22DD92
var lineGraphic:Graphic=lineLayer.getChildByName("line") as Graphic || new Graphic();
lineGraphic.name="line";
lineGraphic.geometry=line;
lineGraphic.symbol=new SimpleLineSymbol('solid', defaultDrawColor, 1, 5);
if (!lineLayer.getChildByName("line"))
lineLayer.add(lineGraphic);
pointGraphic.addEventListener(MouseEvent.MOUSE_UP, enddrag);
pointGraphic.addEventListener(MouseEvent.MOUSE_DOWN, startdrag);
// map.removeEventListener(MapMouseEvent.MAP_CLICK,test_clickHandler);
}
/**
* 开始拖动
* @param event
*/
private function startdrag(event:MouseEvent):void
{
map.removeEventListener(MapMouseEvent.MAP_CLICK, test_clickHandler);
var component:UIComponent=event.currentTarget as UIComponent;
component.startDrag();
}
/**
* 结束拖动
* @param event
*/
private function enddrag(event:MouseEvent):void
{
var component:Graphic=event.currentTarget as Graphic
component.stopDrag();
var m:MapPoint=map.toMapFromStage(event.stageX, event.stageY);
component.toolTip=m.x + "," + m.y;
map.infoWindow.show(m);
component.geometry=m;
trace(component.name);
var idx:int=pointLayer.getChildIndex(component);
idx=idx < 1 ? 0 : idx;
line.setPoint(idx, idx, m);
// lineGraphic.symbol=new SimpleLineSymbol('solid', 0x0000FF, 1, 5);
pointLayer.refresh();
lineLayer.refresh();
map.addEventListener(MapMouseEvent.MAP_CLICK, test_clickHandler);
}
/**
* 改变颜色
* @param event
*/
protected function cpLine_changeHandler(event:ColorPickerEvent):void
{
defaultDrawColor=cpLine.selectedColor;
if (lineLayer.getChildByName("line"))
{
(lineLayer.getChildByName("line") as Graphic).symbol=new SimpleLineSymbol('solid', defaultDrawColor, 1, 5);
lineLayer.refresh();
}
}