路径分析,可以拖动上面的节点,动态修改(转帖自heyubingzju)

Flex下路径分析随机拖动,实现了类似Google map中能够拖动路径并重新实时计算的效果。为了减轻客户端与服务器端的不断交互,因为事实上用户拖动时候并非需要每一步都实时计算,而是在某一点停留超过一给定时间的时候才会重新计算,因此在程序中设置了一个Timer,如果鼠标停留在某一位置超过给定时间,就会向服务器端发送计算的请求,并重新绘制结果。

[javascript]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application  
  3.     xmlns:mx="http://www.adobe.com/2006/mxml"  
  4.     xmlns:esri="http://www.esri.com/2008/ags">  
  5.   
  6.     <mx:Script>  
  7.         <![CDATA[  
  8.             import com.esri.ags.tasks.DirectionsFeatureSet;  
  9.             import com.esri.ags.layers.GPResultImageLayer;  
  10.             import com.esri.ags.geometry.MapPoint;  
  11.             import mx.controls.Alert;  
  12.             import com.esri.ags.tasks.RouteResult;  
  13.             import com.esri.ags.Graphic;  
  14.             import com.esri.ags.events.MapMouseEvent;  
  15.             import com.esri.ags.events.RouteEvent;  
  16.             import com.esri.ags.tasks.FeatureSet;  
  17.             import mx.rpc.events.FaultEvent;  
  18.   
  19.             [Bindable]  
  20.             private var stops:FeatureSet = new FeatureSet([]);  
  21.   
  22.             private var lastRoute:Graphic;  
  23.               
  24.             private var routeOverStop:Graphic = null;  
  25.               
  26.             private var moveRoutePoint:Graphic = null;  
  27.               
  28.             private var coorComTimer:Timer = new Timer(500);  
  29.               
  30.             private var routeOverStart:Boolean = false;  
  31.               
  32.             [Bindable]  
  33.             private var directionsFS:DirectionsFeatureSet;  
  34.   
  35.             private var segmentGraphic:Graphic;  
  36.               
  37.             private const NL:String = "\n";  
  38.               
  39.   
  40.             private function mapClickHandler(event:MapMouseEvent):void  
  41.             {  
  42.                 if (stops.features.length >= 2) return;  
  43.                   
  44.                 var stop:Graphic = new Graphic(event.mapPoint, stopSymbol);  
  45.                 graphicsLayer.add(stop);  
  46.                 if (stops.features.length < 2)  
  47.                     stops.features.push(stop);  
  48.                 else  
  49.                 {  
  50.                     var endstop:Graphic = stops.features.pop();  
  51.                     stops.features.push(stop);  
  52.                     stops.features.push(endstop);                  
  53.                 }                                    
  54.                 if (stops.features.length > 1)  
  55.                 {  
  56.                     routeTask.solve(routeParams);  
  57.                 }  
  58.             }  
  59.               
  60.             private function routeMouseOverHandler(event:MouseEvent):void  
  61.             {  
  62.                 lastRoute.symbol = routeMouseOverSymbol;  
  63.                 var mapPt:MapPoint = map.toMapFromStage(event.stageX, event.stageY);  
  64.                 var overStop:Graphic = new Graphic(mapPt, routeOverSymbol);               
  65.                 graphicsLayer.remove(routeOverStop);  
  66.                 routeOverStop = overStop;  
  67.                 graphicsLayer.remove(moveRoutePoint);  
  68.                 graphicsLayer.add(routeOverStop);                 
  69.                 map.panEnabled = false;  
  70.             }  
  71.               
  72.             private function routeMouseOutHandler(event:MouseEvent):void  
  73.             {  
  74.                 graphicsLayer.remove(routeOverStop);  
  75.                 routeOverStop = null;  
  76.                 map.panEnabled = true;  
  77.             }  
  78.               
  79.             private function mouseDownHandler(event:MouseEvent):void  
  80.             {  
  81.                   
  82.                 if (routeOverStop == nullreturn;  
  83.                 routeOverStart = true;  
  84.                 //Alert.show("Mousedown");  
  85.                 coorComTimer.addEventListener(TimerEvent.TIMER, coorCompare);  
  86.                 map.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);  
  87.                 map.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);  
  88.             }  
  89.               
  90.             private function mouseMoveHandler(event:MouseEvent):void  
  91.             {  
  92.                 lastRoute.symbol = routeSymbol;  
  93.                 var mapPt:MapPoint = map.toMapFromStage(event.stageX, event.stageY);  
  94.                 var overStop:Graphic = new Graphic(mapPt, routeOverSymbol);  
  95.                 graphicsLayer.remove(moveRoutePoint);  
  96.                 moveRoutePoint = overStop;  
  97.                 graphicsLayer.remove(routeOverStop);  
  98.                 graphicsLayer.add(moveRoutePoint);      
  99.                  if (stops.features.length == 2)                     
  100.                 {  
  101.                     var endstop:Graphic = stops.features.pop();  
  102.                     stops.features.push(moveRoutePoint);  
  103.                     stops.features.push(endstop);                  
  104.                 }  
  105.                 else  
  106.                 {  
  107.                     if (routeOverStart)  
  108.                     {  
  109.                         var endstop:Graphic = stops.features.pop();  
  110.                         stops.features.push(moveRoutePoint);  
  111.                         stops.features.push(endstop);   
  112.                         routeOverStart = false;  
  113.                     }  
  114.                     else  
  115.                     {  
  116.                         var endfirststop:Graphic = stops.features.pop();  
  117.                         var endsecondstop:Graphic = stops.features.pop();  
  118.                         stops.features.push(moveRoutePoint);  
  119.                         stops.features.push(endfirststop);  
  120.                     }  
  121.                 }      
  122.                 coorComTimer.stop();  
  123.                 coorComTimer.start();  
  124.             }  
  125.               
  126.             private function coorCompare(event:TimerEvent):void  
  127.             {  
  128.                 if (stops.features.length > 1)  
  129.                 {  
  130.                     routeTask.solve(routeParams);  
  131.                 }  
  132.             }  
  133.               
  134.             private function mouseUpHandler(event:MouseEvent):void  
  135.             {  
  136.                 var i:int = 1;  
  137.                 var textCntl:Text;  
  138.                 theDirections.removeAllChildren();  
  139.                 for each (var feature:Graphic in directionsFS.features)  
  140.                 {  
  141.                     textCntl = new Text();  
  142.                     textCntl.percentWidth = 100;  
  143.                     textCntl.selectable = false;  
  144.                     textCntl.text = i + ". " + feature.attributes.text;  
  145.                     if (i > 1 && i < directionsFS.features.length)  
  146.                     {  
  147.                         textCntl.text += " (" + formatDistance(feature.attributes.length, "miles");  
  148.                         var time:String = formatTime(feature.attributes.time);  
  149.                         if (time != "")  
  150.                         {  
  151.                             textCntl.text += ", " + time;  
  152.                         }  
  153.                         textCntl.text += ")";  
  154.                     }  
  155.                     textCntl.addEventListener(MouseEvent.CLICK, directionsSegmentClickHandler, false, 0, true);  
  156.                     theDirections.addChild(textCntl);  
  157.                     i++;  
  158.                 }  
  159.                 theDirections.toolTip = "Click individual segment to zoom to that segment.";  
  160.                 theSummary.toolTip = "Click to zoom to full route";  
  161.                 theRouteName.toolTip = "Click to zoom to full route";  
  162.                   
  163.                   
  164.                 zoomToFullRoute();    
  165.                 coorComTimer.stop();  
  166.                 coorComTimer.removeEventListener(TimerEvent.TIMER, coorCompare);  
  167.                 map.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);  
  168.                 map.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);  
  169.             }  
  170.               
  171.   
  172.             private function solveCompleteHandler(event:RouteEvent):void  
  173.             {  
  174.                 var routeResult:RouteResult = event.routeSolveResult.routeResults[0];  
  175.                 directionsFS = routeResult.directions;  
  176.                   
  177.                 routeResult.route.symbol = routeSymbol;  
  178.                 graphicsLayer.remove(lastRoute);  
  179.                 lastRoute = routeResult.route;  
  180.                 //snap the route when the mouse over the route  
  181.                 lastRoute.addEventListener(MouseEvent.MOUSE_OVER, routeMouseOverHandler);  
  182.                 lastRoute.toolTip = routeResult.routeName;  
  183.                 if (routeResult.stops && routeResult.stops.length > 0)  
  184.                 {  
  185.                     lastRoute.toolTip += " with " + routeResult.stops.length + "stops";  
  186.                 }  
  187.                 if (routeResult.route.attributes.Total_Time)  
  188.                 {  
  189.                     lastRoute.toolTip += " in " + Math.round(Number(routeResult.route.attributes.Total_Time)) + " minutes.";  
  190.                 }  
  191.                 graphicsLayer.add(lastRoute);                  
  192.             }  
  193.   
  194.             private function faultHandler(event:FaultEvent):void  
  195.             {  
  196.                 Alert.show(event.fault.faultString, "Routing Error");  
  197.                 // remove the last stop  
  198.                 graphicsLayer.remove(stops.features.pop());  
  199.             }  
  200.               
  201.             private function zoomToFullRoute():void  
  202.             {  
  203.                 if (segmentGraphic)  
  204.                 {  
  205.                     graphicsLayer.remove(segmentGraphic);  
  206.                     segmentGraphic = null;  
  207.                 }  
  208.                 map.extent = directionsFS.extent;  
  209.                 if (!map.extent.containsExtent(directionsFS.extent))  
  210.                 {  
  211.                     map.level--; // make sure the whole extent is visible  
  212.                 }  
  213.             }  
  214.   
  215.             private function formatDistance(dist:Number, units:String):String  
  216.             {  
  217.                 var result:String = "";  
  218.   
  219.                 var d:Number = Math.round(dist * 100) / 100;  
  220.   
  221.                 if (d != 0)  
  222.                 {  
  223.                     result = d + " " + units;  
  224.                 }  
  225.   
  226.                 return result;  
  227.             }  
  228.               
  229.             private function formatTime(time:Number):String  
  230.             {  
  231.                 var result:String;  
  232.   
  233.                 var hr:Number = Math.floor(time / 60);  
  234.                 var min:Number = Math.round(time % 60);  
  235.   
  236.                 if (hr < 1 && min < 1)  
  237.                 {  
  238.                     result = "";  
  239.                 }  
  240.                 else if (hr < 1 && min < 2)  
  241.                 {  
  242.                     result = min + " minute";  
  243.                 }  
  244.                 else if (hr < 1)  
  245.                 {  
  246.                     result = min + " minutes";  
  247.                 }  
  248.                 else  
  249.                 {  
  250.                     result = hr + " hour(s) " + min + " minute(s)";  
  251.                 }  
  252.   
  253.                 return result;  
  254.             }  
  255.               
  256.             private function directionsSegmentClickHandler(event:MouseEvent):void  
  257.             {  
  258.                 var textCntl:Text = event.currentTarget as Text;  
  259.                 var segment:Graphic = directionsFS.features[parseInt(textCntl.text) - 1];  
  260.                 map.extent = segment.geometry.extent;  
  261.                 if (!map.extent.containsExtent(segment.geometry.extent))  
  262.                 {  
  263.                     map.level--; // make sure the whole extent is visible  
  264.                 }  
  265.   
  266.                 if (!segmentGraphic)  
  267.                 {  
  268.                     segmentGraphic = new Graphic(segment.geometry, segmentSymbol);  
  269.                     graphicsLayer.add(segmentGraphic);  
  270.                 }  
  271.                 else  
  272.                 {  
  273.                     segmentGraphic.geometry = segment.geometry;  
  274.                 }  
  275.             }  
  276.         ]]>  
  277.     </mx:Script>  
  278.   
  279.     <!-- start declarations -->  
  280.   
  281.     <esri:RouteTask id="routeTask"  
  282.         concurrency="last"  
  283.         fault="faultHandler(event)"  
  284.         solveComplete="solveCompleteHandler(event)"          
  285.         url="http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route"  
  286.     />  
  287.   
  288.     <esri:RouteParameters id="routeParams" findBestSequence="true" preserveFirstStop="true" preserveLastStop="true" stops="{stops}" returnDirections="true"/>  
  289.   
  290.     <esri:SimpleMarkerSymbol id="stopSymbol" size="15" style="triangle">  
  291.         <esri:SimpleLineSymbol width="4"/>  
  292.     </esri:SimpleMarkerSymbol>  
  293.       
  294.     <esri:SimpleMarkerSymbol id="routeOverSymbol" size="10" style="circle" color="0xFFFFFF" alpha="0.7">  
  295.         <esri:SimpleLineSymbol width="2"/>  
  296.     </esri:SimpleMarkerSymbol>  
  297.   
  298.     <esri:SimpleLineSymbol id="routeSymbol" color="0x0000FF" alpha="0.3" width="5"/>  
  299.     <esri:SimpleLineSymbol id="routeMouseOverSymbol" color="0x0000FF" alpha="0.7" width="6"/>  
  300.     <esri:SimpleLineSymbol id="segmentSymbol" color="0xFF0000" alpha="0.5" width="8"/>  
  301.   
  302.     <!-- end declarations -->  
  303.     <mx:HBox width="100%" height="100%">  
  304.         <esri:Map id="map" mapClick="mapClickHandler(event)" mouseDown="mouseDownHandler(event)">  
  305.             <esri:extent>  
  306.                 <esri:Extent xmin="-117.22" ymin="34.04" xmax="-117.17" ymax="34.07">  
  307.                     <esri:SpatialReference wkid="4326"/>  
  308.                 </esri:Extent>  
  309.             </esri:extent>  
  310.             <esri:ArcGISTiledMapServiceLayer   
  311.                 url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>  
  312.             <esri:GraphicsLayer id="graphicsLayer"/>  
  313.             <esri:GraphicsLayer id="routeOverGraphicLayer"/>  
  314.         </esri:Map>  
  315.           
  316.         <mx:VBox width="200" height="100%" backgroundColor="white">  
  317.             <mx:Text id="theRouteName"  
  318.                 click="zoomToFullRoute()"  
  319.                 fontWeight="bold"  
  320.                 text="{directionsFS.routeName}"  
  321.                 toolTip="Click to zoom to full route"  
  322.                 width="100%"/>  
  323.             <mx:Text id="theSummary"  
  324.                 click="zoomToFullRoute()"  
  325.                 text="Total Distance: {formatDistance(directionsFS.totalLength, 'miles')}{NL}Total Time: {formatTime(directionsFS.totalTime)}"  
  326.                 width="100%"/>  
  327.             <mx:HRule width="100%"/>  
  328.             <mx:VBox id="theDirections" width="100%" height="100%" minHeight="0">  
  329.                 <mx:Label text="Click 'Get Directions' above to display driving directions here..."/>  
  330.             </mx:VBox>  
  331.          </mx:VBox>  
  332.      </mx:HBox>  
  333. </mx:Application>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值