ARCGIS viewer入门(7)扩展arcgis server flex API功能

25 篇文章 0 订阅

       所有的基本功能,最终将回到FLEX API。flexviewer是上层的一套优秀框架而已。

(1)query方法(支持图查属性和属性查图

    图查属性

            private function doQuery(geom:Geometry):void
            {
                try
                {
                    var query:Query = new Query();
                    query.spatialRelationship = spatialRel.selectedItem.data;
                    query.geometry = geom;
                    query.returnGeometry = true;
                    queryTask.execute(query, new AsyncResponder(onResult, onFault));


                    function onResult(featureSet:FeatureSet, token:Object = null):void
                    {
                        if (featureSet.features.length > 0)
                        {
                            for each (var myGraphic:Graphic in featureSet.features)
                            {
                                myGraphicsLayer.add(myGraphic);
                            }
                       for each (var myGraphic:Graphic in featureSet.features)
                      {
                    // only add features that are not already in the graphics layer
                    var graphicID:String = myGraphic.attributes.CITY_NAME + "." + myGraphic.attributes.FID;
                    if (!hashmapOfExistingGraphics[graphicID]) // New feature (not already added to graphics layer)
                    {
                        hashmapOfExistingGraphics[graphicID] = 1;
                        myGraphic.id = graphicID;
                        myGraphic.toolTip = myGraphic.attributes.CITY_NAME;
                        myGraphicsLayer.add(myGraphic);
                    }
                }


                        }
                        else
                        {
                            Alert.show("No parcels were found", "Try something else");
                        }}
                    function onFault(info:Object, token:Object = null):void
                    {
                        Alert.show(info.faultString + "\n\n" + info.faultDetail, "queryTask fault " + info.faultCode);
                    }
                }
                catch (error:Error)
                {
                    Alert.show(error.toString(), "myGeometryService_simplifyCompleteHandler error");
                }
            }


            protected function queryTask_faultHandler(event:FaultEvent):void
            {
                Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "QueryTask Fault " + event.fault.faultCode);

            }

属性查图:

    /*属性查图  */
            private function queryFeaturesText():void
            {
                hideInfoWindow();


                var i:int = cboLayerText.selectedIndex;
                queryLayer = configSearchText[i].layer;


                if (queryLayer && !queryLayer.loaded)
                {
                    queryLayer.addEventListener(LayerEvent.LOAD, queryLayer_loadHandler);
                    function queryLayer_loadHandler(event:LayerEvent):void
                    {
                        queryFeaturesText()
                    }
                    return;
                }


                queryExpr = configSearchText[i].expr;
                queryFields = configSearchText[i].fields;
                queryTitleField = configSearchText[i].titlefield;
                queryLinkField = configSearchText[i].linkfield;
                queryLinkAlias = configSearchText[i].linkalias;


                if (queryLayer && txtSearch.text)
                {
                    var query:Query = new Query();
                    var expr:String = queryExpr.replace(/\[value\]/g, txtSearch.text);


                    query.where = expr;
                    query.outSpatialReference = map.spatialReference;
                    queryLayer.queryFeatures(query, new AsyncResponder(onResult, onFault, queryFields));
                    showMessage(loadingLabel, true);
                    showStateResults();


                    function onResult(featureSet:FeatureSet, token:XMLList = null):void
                    {
                        try
                        {
                            searchResultAC = createSearchResults(featureSet, token);


                            addSharedData(widgetTitle, searchResultAC);
                            if (featureSet.features.length < 1)
                            {
                                showMessage(noResultLabel, false);
                            }
                            else
                            {
                                showMessage(selectionLabel + " " + featureSet.features.length, false);
                            }
                        }
                        catch (error:Error)
                        {
                            showMessage(error.message, false);
                        }
                    }


                    function onFault(info:Object, token:Object = null):void
                    {
                        showMessage(info.toString(), false);
                    }
                }
            }

(2)  identifyTask方法

<?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:esri="http://www.esri.com/2008/ags"
               pageTitle="Identify Features on the Map">
    <fx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.events.MapMouseEvent;
            import com.esri.ags.geometry.Geometry;
            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.rpc.AsyncResponder;


            [Bindable]private var lastIdentifyResultGraphic:Graphic;


            private function mapClickHandler(event:MapMouseEvent):void
            {
                clickGraphicsLayer.clear();


                var identifyParams:IdentifyParameters = new IdentifyParameters();
                identifyParams.returnGeometry = true;
                identifyParams.tolerance = 3;
                identifyParams.width = myMap.width;
                identifyParams.height = myMap.height;
                identifyParams.geometry = event.mapPoint;
                identifyParams.mapExtent = myMap.extent;
                identifyParams.spatialReference = myMap.spatialReference;


                var clickGraphic:Graphic = new Graphic(event.mapPoint, clickPtSym);
                clickGraphicsLayer.add(clickGraphic);


                identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, clickGraphic));
            }


            private function myResultFunction(results:Array, clickGraphic:Graphic = null):void
            {
                if (results && results.length > 0)
                {
                    var result:IdentifyResult = results[0];
                    var resultGraphic:Graphic = result.feature;
                    switch (resultGraphic.geometry.type)
                    {
                        case Geometry.MAPPOINT:
                        {
                            resultGraphic.symbol = smsIdentify;
                            break;
                        }
                        case Geometry.POLYLINE:
                        {
                            resultGraphic.symbol = slsIdentify;
                            break;
                        }
                        case Geometry.POLYGON:
                        {
                            resultGraphic.symbol = sfsIdentify;
                            break;
                        }
                    }
                    lastIdentifyResultGraphic = resultGraphic;


                    // update clickGraphic (from mouse click to returned feature)
                    clickGraphic.symbol = new InfoSymbol(); // use default renderer
                    clickGraphic.attributes = resultGraphic.attributes;
                }
            }


            private function myFaultFunction(error:Object, clickGraphic:Graphic = null):void
            {
                Alert.show(String(error), "Identify Error");
            }
        ]]>
    </fx:Script>


    <fx:Declarations>
        <!-- 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"/>


        <!-- Symbol for Identify Result as Polygon -->
        <esri:SimpleFillSymbol id="sfsIdentify"/>


        <!-- Identify Task -->
        <esri:IdentifyTask id="identifyTask"
                           concurrency="last"
                           url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"/>
    </fx:Declarations>


    <esri:Map id="myMap" mapClick="mapClickHandler(event)">
        <esri:extent>
            <esri:WebMercatorExtent minlon="-120" minlat="30" maxlon="-100" maxlat="50"/>
        </esri:extent>
        <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
        <esri:GraphicsLayer graphicProvider="{lastIdentifyResultGraphic}"/>
        <esri:GraphicsLayer id="clickGraphicsLayer"/>
    </esri:Map>


</s:Application>


(3)FindTask方法

<?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"
               pageTitle="Find features in Map Layers">
     
    <s:layout>
        <s:VerticalLayout horizontalAlign="center"/>
    </s:layout>

    <fx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.events.FindEvent;
            import com.esri.ags.geometry.Geometry;

            private function doFind():void
            {
                findTask.execute(myFindParams);
            }

            private function executeCompleteHandler(event:FindEvent):void
            {
                myGraphicsLayer.clear();
                var graphic:Graphic;
                resultSummary.text = "Found " + event.findResults.length + " results.";
                var resultCount:int = event.findResults.length;
                for (var i:Number = 0; i < resultCount; i++)
                {
                    graphic = event.findResults[i].feature;
                    graphic.toolTip = event.findResults[i].foundFieldName + ": " + event.findResults[i].value;

                    switch (graphic.geometry.type)
                    {
                        case Geometry.MAPPOINT:
                        {
                            graphic.symbol = smsFind;
                            break;
                        }
                        case Geometry.POLYLINE:
                        {
                            graphic.symbol = slsFind;
                            break;
                        }
                        case Geometry.POLYGON:
                        {
                            graphic.symbol = sfsFind;
                            break;
                        }
                    }
                    myGraphicsLayer.add(graphic);
                }
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Symbol for Find Result as Polyline -->
        <esri:SimpleLineSymbol id="slsFind"
                               width="3"
                               alpha="0.9"
                               color="0xFFFF00"
                               style="solid"/>

        <!-- Symbol for Find Result as Point -->
        <esri:SimpleMarkerSymbol id="smsFind"
                                 alpha="0.9"
                                 color="0xFFFF00"
                                 size="11"
                                 style="square">
            <esri:SimpleLineSymbol color="0x000000"/>
        </esri:SimpleMarkerSymbol>

        <!-- Symbol for Find Result as Polygon -->
        <esri:SimpleFillSymbol id="sfsFind"
                               alpha="0.7"
                               color="0xFFFF00"/>

        <!-- Find Task -->
        <esri:FindTask id="findTask"
                       executeComplete="executeCompleteHandler(event)"
                       url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"/>
        <esri:FindParameters id="myFindParams"
                             contains="true"
                             layerIds="[0,1,2]"
                             outSpatialReference="{map.spatialReference}"
                             returnGeometry="true"
                             searchFields="[CITY_NAME,NAME,SYSTEM,STATE_ABBR,STATE_NAME]"
                             searchText="{fText.text}"/>
    </fx:Declarations>

    <s:BorderContainer width="100%" height="40"
                       backgroundColor="0xDDDDFF">
        <s:layout>
            <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle"/>
        </s:layout>
        <s:Label text="Search for names of States, Cities, and Rivers:"/>
        <s:TextInput id="fText"
                     enter="doFind()"
                     maxWidth="400"
                     text="Paradise"/>
        <s:Button click="doFind()" label="Find"/>
    </s:BorderContainer>

    <s:Label id="resultSummary" height="15"/>

    <mx:VDividedBox width="100%" height="100%">
        <esri:Map id="map">
            <esri:extent>
                <esri:Extent xmin="-126" ymin="24" xmax="-67" ymax="50">
                    <esri:SpatialReference wkid="4326"/>
                </esri:Extent>
            </esri:extent>
            <esri:ArcGISDynamicMapServiceLayer url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"/>
            <esri:GraphicsLayer id="myGraphicsLayer"/>
        </esri:Map>
        <mx:DataGrid width="100%" height="40%"
                     dataProvider="{findTask.executeLastResult}">
            <mx:columns>
                <mx:DataGridColumn width="70"
                                   dataField="layerId"
                                   headerText="Layer ID"/>
                <mx:DataGridColumn dataField="layerName" headerText="Layer Name"/>
                <mx:DataGridColumn dataField="foundFieldName" headerText="Found Field Name"/>
                <mx:DataGridColumn dataField="value" headerText="Found Field Value"/>
            </mx:columns>
        </mx:DataGrid>
    </mx:VDividedBox>

</s:Application>


(4)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值