<?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" backgroundColor="0xEEEEEE" pageTitle="Query, then zoom to results"> <s:layout> <s:VerticalLayout gap="0"/> </s:layout> <fx:Script> <![CDATA[ import com.esri.ags.FeatureSet; import com.esri.ags.utils.GraphicUtil; import mx.collections.ArrayCollection; import mx.collections.ArrayList; import mx.controls.Alert; import mx.rpc.AsyncResponder; private function doQuery():void { Number(fText.text); // clear the graphics layer myGraphicsLayer.clear(); queryTask.execute(query, new AsyncResponder(onResult, onFault)); function onResult(featureSet:FeatureSet, token:Object = null):void { if (featureSet.features.length == 0) { Alert.show("No States found. Please try again."); } else { var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(featureSet.features); if (graphicsExtent) { map.extent = graphicsExtent; } } } function onFault(info:Object, token:Object = null):void { Alert.show(info.toString()); } } ]]> </fx:Script> <fx:Declarations> <!-- Symbol for Query Result as Polygon --> <esri:SimpleFillSymbol id="sfs" alpha="0.7"/> <!-- Layer with US States --> <esri:QueryTask id="queryTask" url="http://192.168.0.11:6080/arcgis/rest/services/STEARWEBMAPTEST/MapServer/0"/> <esri:Query id="query" outSpatialReference="{map.spatialReference}" returnGeometry="true" where="StateID = {Number(fText.text)}"> </esri:Query> <s:NumberFormatter id="numberFormatter"/> </fx:Declarations> <s:controlBarLayout> <s:VerticalLayout gap="10" paddingBottom="7" paddingLeft="10" paddingRight="10" paddingTop="7"/> </s:controlBarLayout> <s:controlBarContent> <s:RichText width="100%"> This sample demonstrates how to use the QueryTask to search for features in a specific layer in a map service based upon a string value. If the query returns a result, the GraphicUtil class is used to calculate the extent of the returned features and the map's extent is then set to that extent. </s:RichText> <s:HGroup width="100%" verticalAlign="baseline"> <s:Label text="Search for U.S. States:"/> <s:TextInput id="fText" enter="doQuery()" /> <s:Button click="doQuery()" label="Search"/> </s:HGroup> </s:controlBarContent> <esri:Map id="map"> <esri:extent> <esri:Extent xmin="-1.09742216352E7" ymin="3006114.7661000006" xmax="-1.08446320657E7" ymax="3100223.793400001"> <esri:SpatialReference wkid="102100"/> </esri:Extent> </esri:extent> <esri:ArcGISDynamicMapServiceLayer url="http://192.168.0.11:6080/arcgis/rest/services/STEARWEBMAPTEST/MapServer" /> <esri:GraphicsLayer id="myGraphicsLayer" graphicProvider="{queryTask.executeLastResult.features}" symbol="{sfs}"/> </esri:Map> </s:Application>
Solved! Go to Solution.
private function executeCompleteHandler(event:FindEvent):void { myGraphicsLayer.clear(); //resultSummary.text = "Found " + event.findResults.length + " results."; myGraphicsLayer.symbol = smsFind; var resultCount:int = event.findResults.length; if (resultCount == 0) { Alert.show("No State ID found. Please change your search."); } else { // add feature as graphic to graphics layer for (var i:int = 0; i < resultCount; i++) { var graphic:Graphic = FindResult(event.findResults).feature; graphic.toolTip = event.findResults.foundFieldName + ": " + event.findResults.value; myGraphicsLayer.add(graphic); } // zoom to extent of all features var graphicProvider:ArrayCollection = myGraphicsLayer.graphicProvider as ArrayCollection; if (graphicProvider.length == 1 && graphicProvider[0].geometry.type == Geometry.MAPPOINT){ var mp:MapPoint = graphicProvider[0].geometry as MapPoint; mainMap.zoom(1 / 16, mp); mainMap.centerAt(mp); }else{ var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(graphicProvider.toArray()); if(graphicsExtent){ mainMap.zoomTo(graphicsExtent.expand(1.4)); }else{ var mp2:MapPoint = graphicProvider[0].geometry as MapPoint; mainMap.zoom(1 / 16, mp2); mainMap.centerAt(mp2); } } }
<esri:GraphicsLayer id="myGraphicsLayer" graphicProvider="{queryTask.executeLastResult.features}" symbol="{sfs}"/>
private function doFind():void { findTask.execute(myFindParams); } private function executeCompleteHandler(event:FindEvent):void { myGraphicsLayer.clear(); resultSummary.text = "Found " + event.findResults.length + " results."; myGraphicsLayer.symbol = smsFind; var resultCount:int = event.findResults.length; if (resultCount == 0) { Alert.show("No State ID found. Please change your search."); } else { // add feature as graphic to graphics layer for (var i:int = 0; i < resultCount; i++) { var graphic:Graphic = FindResult(event.findResults).feature; graphic.toolTip = event.findResults.foundFieldName + ": " + event.findResults.value; myGraphicsLayer.add(graphic); } // zoom to extent of all features var graphicProvider:ArrayCollection = myGraphicsLayer.graphicProvider as ArrayCollection; var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(graphicProvider.toArray()); mainMap.extent = graphicsExtent.expand(1.4); // zoom out a little } }
<esri:SimpleMarkerSymbol id="smsFind" alpha="0.9" color="#0000FF" size="11" style="square"> </esri:SimpleMarkerSymbol> <esri:FindTask id="findTask" executeComplete="executeCompleteHandler(event)" url="http://192.168.0.11:6080/arcgis/rest/services/STEARWEBMAPTEST/MapServer"/> <esri:FindParameters id="myFindParams" contains="true" layerIds="[0]" outSpatialReference="{mainMap.spatialReference}" returnGeometry="true" searchFields="[StateID]" searchText="{qText.text}"/> <esri:Map id="mainMap" x="0" y="10" height="100%" width="100%" logoVisible="false" > <esri:extent> <esri:Extent xmax="-1.08446320657E7" ymax="3100223.793400001" xmin="-1.09742216352E7" ymin="3006114.7661000006"> <esri:SpatialReference wkid="102100" > </esri:SpatialReference> </esri:Extent> </esri:extent> <esri:ArcGISTiledMapServiceLayer url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" /> <esri:ArcGISDynamicMapServiceLayer url="http://192.168.0.11:6080/arcgis/rest/services/STEARWEBMAPTEST/MapServer" /> <esri:FeatureLayer id="points" url="http://192.168.0.11:6080/arcgis/rest/services/STEARWEBMAPTEST/FeatureServer/0" outFields="*" /> <esri:GraphicsLayer id="myGraphicsLayer" /> </esri:Map>
private function executeCompleteHandler(event:FindEvent):void { myGraphicsLayer.clear(); //resultSummary.text = "Found " + event.findResults.length + " results."; myGraphicsLayer.symbol = smsFind; var resultCount:int = event.findResults.length; if (resultCount == 0) { Alert.show("No State ID found. Please change your search."); } else { // add feature as graphic to graphics layer for (var i:int = 0; i < resultCount; i++) { var graphic:Graphic = FindResult(event.findResults).feature; graphic.toolTip = event.findResults.foundFieldName + ": " + event.findResults.value; myGraphicsLayer.add(graphic); } // zoom to extent of all features var graphicProvider:ArrayCollection = myGraphicsLayer.graphicProvider as ArrayCollection; if (graphicProvider.length == 1 && graphicProvider[0].geometry.type == Geometry.MAPPOINT){ var mp:MapPoint = graphicProvider[0].geometry as MapPoint; mainMap.zoom(1 / 16, mp); mainMap.centerAt(mp); }else{ var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(graphicProvider.toArray()); if(graphicsExtent){ mainMap.zoomTo(graphicsExtent.expand(1.4)); }else{ var mp2:MapPoint = graphicProvider[0].geometry as MapPoint; mainMap.zoom(1 / 16, mp2); mainMap.centerAt(mp2); } } }