config.xml文件的配置如下:
1 <widget label="资源搜索" icon="assets/images/public_impact_over.png" config="widgets/AdvancedBusiness/AdvancedBusinessWidget.xml" url="widgets/AdvancedBusiness/AdvancedBusinessWidget.swf" />
源代码目录如下:
界面效果:
大概的思路如下:跟地图搜索的思路是一样的,不过是这里唯一变化的是可以勾选多个图层来进行query查询,其实只是进行多次循环查询不同的图层而已,核心是一样的;AdvancedBusinessWidget.xml是资源搜索模块的配置文件,AdvancedBusinessWidget.mxml是widget;AdvancedBusinessWidget.xml主要是配置资源图层搜索的服务url,用来进行query查询用,这个模块的核心其实就是调用arcgis api的query类以及queryTask
AdvancedBusinessWidget.xml:
1 <?xml version="1.0" ?> 2 <configuration label="Louisville Parcels and Police"> 3 <layers> 4 <layer> 5 <name>危险隐患</name> 6 <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/0 7 </url> 8 <expression>NAME like '%[value]%'</expression> 9 <textsearchlabel>按照名称搜索</textsearchlabel> 10 <titlefield>NAME</titlefield> 11 <linkfield></linkfield> 12 <fields all="true"> 13 <field name="NAME" /> 14 </fields> 15 <checked>true</checked> 16 </layer> 17 <layer> 18 <name>应急专家</name> 19 <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/1 20 </url> 21 <expression>NAME like '%[value]%'</expression> 22 <textsearchlabel>按照名称搜索</textsearchlabel> 23 <titlefield>NAME</titlefield> 24 <linkfield></linkfield> 25 <fields all="true"> 26 <field name="NAME" /> 27 </fields> 28 <checked>false</checked> 29 </layer> 30 <layer> 31 <name>物资装备</name> 32 <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/2 33 </url> 34 <expression>NAME like '%[value]%'</expression> 35 <textsearchlabel>按照名称搜索</textsearchlabel> 36 <titlefield>NAME</titlefield> 37 <linkfield></linkfield> 38 <fields all="true"> 39 <field name="NAME" /> 40 </fields> 41 <checked>false</checked> 42 </layer> 43 <layer> 44 <name>救援队伍</name> 45 <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/3 46 </url> 47 <expression>NAME like '%[value]%'</expression> 48 <textsearchlabel>按照名称搜索</textsearchlabel> 49 <titlefield>NAME</titlefield> 50 <linkfield></linkfield> 51 <fields all="true"> 52 <field name="NAME" /> 53 </fields> 54 <checked>false</checked> 55 </layer> 56 <layer> 57 <name>庇护场所</name> 58 <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/4 59 </url> 60 <expression>NAME like '%[value]%'</expression> 61 <textsearchlabel>按照名称搜索</textsearchlabel> 62 <titlefield>NAME</titlefield> 63 <linkfield></linkfield> 64 <fields all="true"> 65 <field name="NAME" /> 66 </fields> 67 <checked>false</checked> 68 </layer> 69 <layer> 70 <name>保护对象</name> 71 <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/5 72 </url> 73 <expression>NAME like '%[value]%'</expression> 74 <textsearchlabel>按照名称搜索</textsearchlabel> 75 <titlefield>NAME</titlefield> 76 <linkfield></linkfield> 77 <fields all="true"> 78 <field name="NAME" /> 79 </fields> 80 <checked>false</checked> 81 </layer> 82 <!-- <layer> --> 83 <!-- <name>应急仓库</name> --> 84 <!-- <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/6 --> 85 <!-- </url> --> 86 <!-- <expression>NAME like '%[value]%'</expression> --> 87 <!-- <textsearchlabel>Search by NAME</textsearchlabel> --> 88 <!-- <titlefield>NAME</titlefield> --> 89 <!-- <linkfield></linkfield> --> 90 <!-- <fields all="true"> --> 91 <!-- <field name="NAME" /> --> 92 <!-- </fields> --> 93 <!-- <checked>false</checked> --> 94 <!-- </layer> --> 95 <layer> 96 <name>企业</name> 97 <url>http://localhost:6080/ArcGIS/rest/services/nsBusiness/MapServer/7 98 </url> 99 <expression>NAME like '%[value]%'</expression> 100 <textsearchlabel>按照名称搜索</textsearchlabel> 101 <titlefield>NAME</titlefield> 102 <linkfield></linkfield> 103 <fields all="true"> 104 <field name="NAME" /> 105 </fields> 106 <checked>false</checked> 107 </layer> 108 <layer> 109 <name>全选</name> 110 <url></url> 111 <expression>NAME like '%[value]%'</expression> 112 <textsearchlabel>按照名称搜索</textsearchlabel> 113 <titlefield>NAME</titlefield> 114 <linkfield></linkfield> 115 <fields all="true"> 116 <field name="NAME" /> 117 </fields> 118 <checked>false</checked> 119 </layer> 120 </layers> 121 <zoomscale>10000</zoomscale> 122 </configuration> 123 124 <!-- See Search widget tag reference at http://links.esri.com/searchwidget -->
AdvancedBusinessWidgett.mxml:具体实现部分,我截图部分代码好了,具体的详见flexviewer。主要是有两种方式查询,一种是框选,另一种是关键字查询;框选查询是利用在地图框选(线 面 拉框等等)获取框选的范围Geometry,然后利用当前的Geometry作为queryTask的参数,用于query查询;输入关键字查询是类似的,不过是query的参数把geometry替换为text。
框选模式参照地图搜索部分
关键字查询:
在勾选图层的基础上循环遍历查询
1 private function textFilter():void{ 3 for each(var searchLayer:Object in configSearchGraphicalArr){//configSearchGraphicalArr是勾选的图层数组 if(searchLayer.checked && searchLayer.label!="全选"){ 5 queryFeaturesText(searchLayer); 6 } 7 } 8 }
queryFeaturesText是基于关键字搜索函数:
private function queryFeaturesText(searchLayer:Object):void { hideInfoWindow(); queryLayer = searchLayer.layer; if (queryLayer && !queryLayer.loaded) { queryLayer.addEventListener(LayerEvent.LOAD, queryLayer_loadHandler); queryLayer.addEventListener(LayerEvent.LOAD_ERROR, queryLayer_loadErrorHandler); function queryLayer_loadHandler(event:LayerEvent):void { queryLayer.removeEventListener(LayerEvent.LOAD, queryLayer_loadHandler); queryLayer.removeEventListener(LayerEvent.LOAD_ERROR, queryLayer_loadErrorHandler); queryFeaturesText(queryLayer); } function queryLayer_loadErrorHandler(event:LayerEvent):void { queryLayer.removeEventListener(LayerEvent.LOAD, queryLayer_loadHandler); queryLayer.removeEventListener(LayerEvent.LOAD_ERROR, queryLayer_loadErrorHandler); showLoadErrorMessage(event); } return; } queryExpr = searchLayer.expr; queryFields = searchLayer.fields; queryTitleField = searchLayer.titlefield; queryLinkField = searchLayer.linkfield; queryLinkAlias = searchLayer.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; const supportsServerSideSorting:Boolean = queryLayer.layerDetails && queryLayer.layerDetails.version >= 10.1 && queryLayer.layerDetails.supportsAdvancedQueries; const orderByFields:Array = searchLayer.orderbyfields; if (supportsServerSideSorting && orderByFields) { query.orderByFields = orderByFields; } var queryOptions:Object = { supportsServerSideSorting: supportsServerSideSorting, orderByFields: orderByFields, queryFields: queryFields }; queryLayer.queryFeatures( query, new AsyncResponder(queryFeatures_resultHandler, queryFeatures_faultHandler, queryOptions)); //showMessage(loadingLabel, true); //showStateResults(); function queryFeatures_resultHandler(featureSet:FeatureSet, queryOptions:Object):void { /* var layer:FeatureLayer=FeatureLayer(map.getLayer(searchLayer.label)); layer.featureCollection=new FeatureCollection(featureSet,layer.layerDetails); layer.refresh();*/ var obj:Object=new Object(); obj.featureSet=featureSet; obj.layerName=searchLayer.label; obj.featureLayer=FeatureLayer(map.getLayer(searchLayer.label)); AppEvent.dispatch(BusinessEventType.TEXT_FILTER_EVENT,obj); } function queryFeatures_faultHandler(info:Object, queryOptions:Object):void { showMessage(info.toString(), false); } } } //AppEvent.dispatch(BusinessEventType.TEXT_FILTER_EVENT,obj);这里会触发搜索结果表格事件,把搜索的结果更新到表格来展示
textFilter_Listener是展示搜索结果函数:
private function textFilter_Listener(event:AppEvent):void{ //Alert.show("The event is triggered!"); var featureSet:FeatureSet=event.data.featureSet as FeatureSet; var layerName:String=event.data.layerName;//no use right now var featureLayer:FeatureLayer=event.data.featureLayer as FeatureLayer; var businessTable:CustomAttributeTable=featureLayerToAttributeTable[featureLayer]; businessTable.doSpatialFilter(featureSet); }
public function doSpatialFilter(featureSet:FeatureSet):void{ this._map.removeEventListener(ExtentEvent.EXTENT_CHANGE,extentChanged_Refresh); this._featureSet=featureSet; if(featureSet==null){ if(this.dataGrid&&this.skin){ this.dataGrid.dataProvider=null; this.skin.currentState="noFeaturesInCurrentExtent"; } }else{ if(featureSet.attributes.length>0){ if(this.dataGrid&&this.skin){ this.dataGrid.columns=this.createColumnsFromPopInfo(); this.dataGrid.dataProvider=new ArrayCollection(featureSet.attributes); this.skin.currentState="normal"; } }else{ if(this.dataGrid&&this.skin){ this.dataGrid.dataProvider=null; this.skin.currentState="noFeaturesInCurrentExtent"; } } } }
备注:
GIS技术交流QQ群:432512093