Buffer graphic
View live sample | Download as a zip file
Description
Use "URLRequest("http://sgs.supergeo.com.tw/twmap/Buffer.aspx" + "/SetArea");" to call the buffer.aspx on the server side to perform buffer analysis.
How To Use
Copy and modify the codes below to meet your needs.
Code
                                <?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" minWidth="955" minHeight="600"
               xmlns:sgw="library://www.supergeotek.com/sgw"
               initialize="application1_initializeHandler(event)">
     
    <fx:Script>
 
        <![CDATA[
             
            import Core.ICommand;
            import Core.IDisplayTransformation;
            import Core.ILayer;
            import Core.ITool;
            import Geometry.Envelope;
            import Geometry.LineString;
            import Geometry.LinearRing;
            import Geometry.Point;
            import Geometry.Polygon;
            import MapLayer.MapCachedLayer;
            import MapLayer.MapLayer;
            import MapLayer.MapLayerEvents;
            import MapLayer.MapLayerInner;
            import Tool.*;
            import Tool.FullExtentTool;
            import Tool.Identify;
            import Tool.MapZoomStep;
            import Tool.PanTool;
            import Tool.ZoomLastTool;
            import Tool.ZoomTool;
            import flash.display.Graphics;
            import flash.events.Event;
            import flash.events.IOErrorEvent;
            import flash.events.MouseEvent;
            import flash.net.URLLoader;
            import flash.net.URLRequest;
            import mx.collections.ArrayCollection;
            import mx.containers.Canvas;
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.utils.StringUtil;
            import spark.components.BorderContainer;
            import spark.components.HGroup;
            import spark.components.Label;
            import spark.components.Panel;
            import spark.components.RadioButton;
            import spark.components.RadioButtonGroup;
            import spark.components.VGroup;
             
            private var ary1:ArrayCollection = null;
            private var m_GL_LnP:BorderContainer = null; //permanent line segment
            private var ctsTgt:BorderContainer = null; //permanent line segment
             
            protected function application1_initializeHandler(event:FlexEvent):void
                 
            {
                var pLyr:MapLayer = new MapLayer();
                pLyr.addEventListener(MapLayerEvents.LOADED, maptilelayer1_LoadedHandler);
                pLyr.Name = "SGW";
                pLyr.Title = "SGW";
                pLyr.ResourcePath="http://sgs.supergeo.com.tw/world_rst1/Agent.aspx";
            }
             
             
             
            protected function maptilelayer1_LoadedHandler(vEvent:MapLayerEvents):void
            {
                var pStep:MapZoomStep= new MapZoomStep(oMapBase);
                var pLyr:MapLayer = vEvent.currentTarget as MapLayer;
                var pExt:Envelope = pLyr.Extent;
                oMapBase.AddLayer(pLyr);
                 
                var pTrans:IDisplayTransformation = new LevelTransformation(256 / (pExt.Right - pExt.Left), 0, 15);
                oMapBase.Transformation = pTrans as IDisplayTransformation;
                 
                var pExt1:Envelope = new Envelope(50.911711, 56.743384, 138.434733, -2.919457) as Envelope;
                oMapBase.ZoomMapTo(pExt1);
                 
                oMapBase.RefreshMap(true);
                 
                var pTl1:PanTool = new PanTool(true, true);
                oMapBase.SelectMapTool(pTl1);
                 
                //save the buffer graphics
                ary1 = new ArrayCollection();
                 
                //initial buffer graphic
                m_GL_LnP = new BorderContainer(); //Permanent Line graphic
                m_GL_LnP.left = 0;
                m_GL_LnP.top = 0;
                m_GL_LnP.width = 0;
                m_GL_LnP.height = 0;
                oMapBase.addElement(m_GL_LnP);
                 
                //add map base event
                oMapBase.addEventListener(MapControlEvents.REBUILT,UU);
                 
                //add the symbol for buffer target locate
                ctsTgt = new BorderContainer(); 
                ctsTgt.left = 0;
                ctsTgt.top = 0;
                ctsTgt.width = 0;
                ctsTgt.height = 0;
                oMapBase.addElement(ctsTgt);
                var ctrPt1:Geometry.Point = oMapBase.Transformation.Center;
                var ctrPt2:Geometry.Point = oMapBase.FromMapPoint(ctrPt1.X,ctrPt1.Y);
                ctsTgt.contentGroup.graphics.lineStyle(2, 0x000000, 1.0);
                 
                ctsTgt.contentGroup.graphics.moveTo(ctrPt2.X-10, ctrPt2.Y);
                ctsTgt.contentGroup.graphics.lineTo(ctrPt2.X+10,ctrPt2.Y); 
                 
                ctsTgt.contentGroup.graphics.moveTo(ctrPt2.X, ctrPt2.Y+10);
                ctsTgt.contentGroup.graphics.lineTo(ctrPt2.X,ctrPt2.Y-10); 
            }
             
             
            protected function UU(vEvent:Event):void
            {
                 
                //draw polygon
                m_GL_LnP.contentGroup.graphics.clear();
                m_GL_LnP.contentGroup.graphics.lineStyle(1, 0xFF0000, 2.0);
                 
                for (var k:Number=0;k<ary1.length;k++) 
                { 
                    var pg:Geometry.Polygon = ary1.getItemAt(k) as Geometry.Polygon;
                    DrawPolygon(m_GL_LnP.contentGroup.graphics, pg);
                } 
                m_GL_LnP.contentGroup.graphics.endFill();
                 
            }
             
            protected function pQuery(vEvent:Tool.Identify):void{
            }
             
            protected function button1_clickHandler(event:MouseEvent):void
            {
                //Buffer the center of the map
                for (var i:Number=3;i<6;i++) 
                {
                    var Req:URLRequest = new URLRequest("http://sgs.supergeo.com.tw/twmap/Buffer.aspx" + "/SetArea");
                    Req.method = "POST";
                     
                    var pTrn:IDisplayTransformation = oMapBase.Transformation;
                    var ctpt:Geometry.Point = pTrn.Center;
                    Req.data = "GEOM="+ ctpt.ExportToWKT() + "&Rng=" + i; //in degree unit
                    //send the buffer request , get the buffer graphic back
                     
                    var Ldr:URLLoader = new URLLoader();
                     
                    Ldr.addEventListener(Event.COMPLETE, OnLoaded); //get the buffer , do "OnLoaded"
                    Ldr.addEventListener(IOErrorEvent.IO_ERROR, OnFailed); //fail, go to "OnFailed"
                    Ldr.load(Req);
                }
                 
            }
             
            protected function OnFailed (vEvent:Event):void
            {
                Alert.show("Buffer Failed");
            } 
             
            //Get the buffer graphic and do the query
            protected function OnLoaded (vEvent:Event):void
            {
                 
                var pNode :XML = new XML(vEvent.target.data as String);
                var pGeoms:XMLList = pNode.child("Geometry");
                var pGeom:XML = pGeoms[0];
                var sWKT:String = pGeom.text();
                //convert the buffer text string into polygon
                var pg:Polygon = new Polygon();
                pg.ImportFromWKT(sWKT);
                 
                //push the user click points into the array for redraw the graphic when map refreshes
                ary1.addItem(pg);
                 
                //draw polygon
                m_GL_LnP.contentGroup.graphics.lineStyle(1, 0xFF0000, 1.0);
                 
                DrawPolygon(m_GL_LnP.contentGroup.graphics, pg);
                 
                 
            }
             
            private function DrawPolygon(grphs:Graphics, pPgn:Polygon):void
            {
                 
                DrawRing(grphs, pPgn.ExteriorRing);
                for(var j:int = 0 ; j < pPgn.NumInteriorRings ; j++)
                    DrawRing(grphs, pPgn.GetInteriorRing(j));
            }
             
            private function DrawRing(grphs:Graphics, pLR:LinearRing):void
            {
                 
                if (pLR.NumPoints <= 0)
                    return;
                DrawLineString(grphs, pLR);
                var pt:Geometry.Point = pLR.GetPoint(0);
                var scltpt:Geometry.Point = oMapBase.FromMapPoint(pt.X, pt.Y);
                 
                grphs.lineTo(scltpt.X, scltpt.Y);
            }
             
            private function DrawLineString(grphs:Graphics, pLs:LineString):void
            {
                 
                if (pLs.NumPoints <= 0)
                    return;
                 
                var pt:Geometry.Point = pLs.GetPoint(0);
                //convert to screen points
                var scltpt:Geometry.Point = oMapBase.FromMapPoint(pt.X, pt.Y);
                 
                grphs.moveTo(scltpt.X, scltpt.Y);
                grphs.beginFill(0xFFFF00, 0.23);
                var tmpS1:String = null;
                 
                for(var i:int = 1 ; i < pLs.NumPoints ; i++) 
                {
                    pt = pLs.GetPoint(i);
                    var cltpt:Geometry.Point = oMapBase.FromMapPoint(pt.X, pt.Y);
                    grphs.lineTo(cltpt.X, cltpt.Y);
                }
                grphs.endFill();
                 
            }
        ]]>
         
    </fx:Script>
     
    <fx:Declarations>
         
    </fx:Declarations>
     
    <sgw:MapControl id="oMapBase" width="100%" height="100%">
    </sgw:MapControl>
    <s:Button x="17" y="80" label="Buffer Center" width="125" height="28" click="button1_clickHandler(event)"/>
    <s:Label x="17" y="13" text="SuperGIS Server Flex Website Sample&#xd;--- Buffer" fontWeight="bold" fontSize="30" color="#016B09" backgroundColor="#9ACD32" backgroundAlpha="0.7"/>
</s:Application>