(四)IdentifyTaskを使用してレイヤー属性を問い合わせる

14024 ワード

一、完全なコード
<?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"

               minWidth="955" minHeight="600">

    <fx:Declarations>

        <esri:DrawTool id="drawTool" map="{myMap}" graphicsLayer="{myLayer as GraphicsLayer}"

                       drawEnd="onDrawEnd(event)"/>

        <esri:IdentifyTask id="identifyTask"  url="{serverUrl}"/>

        

        <!-- Symbol for where the user clicked -->

        <esri:SimpleMarkerSymbol id="clickPtSym"

                                 color="0xFF0000"

                                 size="12"

                                 style="x"/>

        

        <!-- Symbol for Identify Result as Polyline -->

        <esri:SimpleLineSymbol id="slsIdentify"

                               width="2"

                               alpha="1"

                               color="0x00FF00"

                               style="solid"/>

        

        <!-- Symbol for Identify Result as Point -->

        <esri:SimpleMarkerSymbol id="smsIdentify"

                                 color="0x00FF00"

                                 size="15"

                                 style="diamond"/>

        <esri:SimpleFillSymbol id="sfsIdentify"/>

    </fx:Declarations>

    

    <fx:Script>

        <![CDATA[

            import com.esri.ags.Graphic;

            import com.esri.ags.events.DrawEvent;

            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.symbols.InfoSymbol;

            import com.esri.ags.tasks.supportClasses.IdentifyParameters;

            import com.esri.ags.tasks.supportClasses.IdentifyResult;

            

            import mx.controls.Alert;

            import mx.controls.DataGrid;

            import mx.events.ItemClickEvent;

            import mx.rpc.AsyncResponder;

            [Bindable]

            private var serverUrl:String="http://localhost:8399/arcgis/rest/services/TestUSA/MapServer";

            

            [Bindable]

            private var lastIdentifyResultGraphic:Graphic;

            

            protected function toolBar_Clicked(event:ItemClickEvent):void{

                switch(event.index){

                    case 0:

                        drawTool.deactivate();

                        break;

                    case 1:

                        drawTool.activate(DrawTool.EXTENT);

                        break;

                }

            }

            

            protected function onDrawEnd(event:DrawEvent):void{

                var identifyParams:IdentifyParameters=new IdentifyParameters();

                identifyParams.returnGeometry=true;

                identifyParams.tolerance=3;

                identifyParams.width = myMap.width;

                identifyParams.height = myMap.height;

                identifyParams.geometry=event.graphic.geometry;

                identifyParams.mapExtent=myMap.extent;

                /**

                 *          ,     ,               (        ),      

                 *      identifyParams.layerOption=IdentifyParameters.LAYER_OPTION_VISIBLE;        

                 *                                                    LAYER_OPTION_ALL;          

                 *                                                    LAYER_OPTION_TOP;         

                 * */

                var layers:Array=new Array();

                layers.push(1); 

                identifyParams.layerIds=layers;



                identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, event.graphic));

            }

            

            //       

            private function myResultFunction(results:Array, clickGraphic:Graphic = null):void{

                resultGraphicsLayer.clear();

                if(results && results.length>0){

                    var attriList:Array=new Array();

                    for each (var identifyResult:IdentifyResult in results)

                    {

                        var resultGraphic:Graphic = identifyResult.feature;

                        //     ,         ,     ,     ,       

                        resultGraphic.symbol = slsIdentify;

                        

                        //        

                        //attriList.push(resultGraphic.attributes);

                        //      

                        attriList.push({"    ":resultGraphic.attributes.TYPE,"    ":resultGraphic.attributes.LENGTH});

                        resultGraphicsLayer.add(resultGraphic);

                    }

                    var sg:DataGrid=new DataGrid();

                    sg.width=200;

                    

                    sg.dataProvider=attriList;

                    myMap.infoWindowContent=sg;

                    myMap.infoWindow.label="      ";

                    myMap.infoWindow.show(getGeomCenter(resultGraphic));

                }

            }

            

            private function myFaultFunction(error:Object, clickGraphic:Graphic = null):void

            {

                Alert.show(String(error), "Identify Error");

            }

            

            //

            private function getGeomCenter(graphic:Graphic):MapPoint

            {

                var pt:MapPoint;

                switch (graphic.geometry.type)

                {

                    case Geometry.MAPPOINT:

                    {

                        pt = graphic.geometry as MapPoint;

                        break;

                    }

                    case Geometry.POLYLINE:

                    {

                        const pl:Polyline = graphic.geometry as Polyline;

                        const pathCount:Number = pl.paths.length;

                        const pathIndex:int = int((pathCount / 2) - 1);

                        const midPath:Array = pl.paths[pathIndex];

                        const ptCount:Number = midPath.length;

                        const ptIndex:int = int((ptCount / 2) - 1);

                        pt = pl.getPoint(pathIndex, ptIndex);

                        break;

                    }

                    case Geometry.POLYGON:

                    {

                        const poly:Polygon = graphic.geometry as Polygon;

                        pt = poly.extent.center;

                        break;

                    }

                }

                return pt;

            }

        ]]>

    </fx:Script>

    <mx:ToggleButtonBar itemClick="toolBar_Clicked(event)">

        <mx:dataProvider>

            <fx:Array>

                <fx:String>  </fx:String>

                <fx:String>    </fx:String>

            </fx:Array>

        </mx:dataProvider>

    </mx:ToggleButtonBar>

    <esri:Map x="0" y="86" width="100%" height="100%" id="myMap">

        <esri:ArcGISDynamicMapServiceLayer url="{serverUrl}" id="myLayer"/>    

        <esri:GraphicsLayer id="resultGraphicsLayer"/>

        <esri:GraphicsLayer id="clickGraphicsLayer"/>

    </esri:Map>    

</s:Application>

二、効果図
(四)使用IdentifyTask查询图层属性