2009-12-10 9 views

Répondre

1

Vous tracez un groupe de lignes reliant les points du polygone.

Par exemple rapide:

function drawPolygon(first, ... rest) { 
    graphics.moveTo(first.x, first.y); 
    for(var i = 0; i < rest.length; i++) { 
     graphics.lineTo(rest[i].x, rest[i].y); 
    } 
    graphics.lineTo(first.x, first.y); 
} 

Peut être quelques erreurs de syntaxe mineures, mais vous voyez l'idée. Vous l'appelez en passant un tas d'objets Point indiquant les points du polygone.

0

Essayez cet exemple

<?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" 
       xmlns:esri="http://www.esri.com/2008/ags"> 
    <fx:Script> 
     <![CDATA[ 
      import com.esri.ags.Graphic; 
      import com.esri.ags.SpatialReference; 
      import com.esri.ags.Units; 
      import com.esri.ags.geometry.Geometry; 
      import com.esri.ags.geometry.MapPoint; 
      import com.esri.ags.geometry.Polygon; 
      import com.esri.ags.geometry.Polyline; 
      import com.esri.ags.utils.GeometryUtil; 

      import mx.utils.StringUtil; 

      private const sr:SpatialReference = new SpatialReference(4326); 

      protected function onCreatePolyline(event:MouseEvent):void 
      { 
       addMessage("Create polyline clicked"); 

       var pts:Array = new Array(); 
       for (var i:int; i < 10; i++) // add 10 random points to path 
       { 
        var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr); 
        pts.push(pt); 
       } 

       var pl:Polyline = new Polyline(new Array(pts), sr); 

       var lengths:Array = GeometryUtil.geodesicLengths(new Array(pl), Units.KILOMETERS); 
       if (lengths != null && lengths.length > 0) 
       { 

        addMessage(StringUtil.substitute("polyline created with length {0} km", lengths[0])); 
       } 

       addGraphic(pl); 
      } 


      protected function onCreatePolygon(event:MouseEvent):void 
      { 
       addMessage("Create polygon clicked"); 
       var pts:Array = new Array(); 
       for (var i:int; i < 10; i++) // add 10 random points to ring 
       { 
        var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr); 
        pts.push(pt); 
       } 

       var pg:Polygon = new Polygon(new Array(pts), sr); 

       var areas:Array = GeometryUtil.geodesicAreas(new Array(pg), Units.SQUARE_KILOMETERS); 
       if (areas != null && areas.length > 0) 
       { 

        addMessage(StringUtil.substitute("polygon created with area {0} km²", Math.abs(areas[0]))); 
       } 

       addGraphic(pg); 
      } 

      private function addMessage(message:String):void 
      { 
       log.text = StringUtil.substitute("> > > {0}\n{1}", message, log.text); 
      } 

      private function addGraphic(geometry:Geometry):void 
      { 
       var gr:Graphic = new Graphic(geometry); 
       grLayer.clear(); 
       var grId:String = grLayer.add(gr); 
       addMessage(StringUtil.substitute("graphic added with id='{0}'", grId)); 
       map.initialExtent = geometry.extent; 
       map.zoomToInitialExtent(); 
      } 

     ]]> 
    </fx:Script> 

    <s:layout> 
     <s:VerticalLayout gap="10" 
          paddingBottom="10" 
          paddingLeft="10" 
          paddingRight="10" 
          paddingTop="10"/> 
    </s:layout> 

    <s:Button label="Create polyline" 
       click="onCreatePolyline(event)"/> 

    <s:Button label="Create polygon" 
       click="onCreatePolygon(event)"/> 

    <s:TextArea id="log" 
       width="100%" 
       height="100%"/> 

    <esri:Map id="map" 
         zoomSliderVisible="false" 
       minHeight="200" 
       width="100%"> 

     <esri:GraphicsLayer id="grLayer" /> 

    </esri:Map> 

</s:Application>