switch(selectedFeatureLayer.layerDetails.geometryType) { case Geometry.POLYLINE: { Alert.show(myPointArray.toString(), myPointArray.length.toString()); pline = new Polyline([myPointArray]); var myGraphicLine:Graphic = new Graphic; myGraphicLine.geometry = pline; myGraphicLine.symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT,0x0000FF, 0.6,2); myGraphicsLayer.add(myGraphicLine); selectedFeatureLayer.applyEdits([myGraphicLine], null , null); break; } case Geometry.POLYGON: { // Get the points array and add the first point at the end of the array to make a polygon var onePointArray:Array = new Array; onePointArray=myPointArray[0]; myPointArray.push([onePointArray]); // Create polygon graphic poly = new Polygon([[myPointArray]]); myGraphicPoly.geometry = poly; myGraphicPoly.symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,0x00FF00, 0.7); myGraphicsLayer.add(myGraphicPoly); selectedFeatureLayer.applyEdits([myGraphicPoly], null , null); myPointArray = new Array; finishAdd.enabled = false; break; } default: { break; } }
Solved! Go to Solution.
<?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>
<?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>