属性查询图形FindTask

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
< html >
< head >
     < meta  charset = "utf-8" >
     < title >属性查询图形</ title >
     < script  src = "../js/jsConfigVS.js" ></ script >
     < link  rel = "stylesheet"  type = "text/css"  href = "../arcgis_js_api/library/3.17/esri/css/esri.css"  />
     < link  rel = "stylesheet"  type = "text/css"  href = "../arcgis_js_api/library/3.17/dijit/themes/claro/claro.css" >
     < link  rel = "stylesheet"  href = "Layout.css"  />
     < script  src = "../arcgis_js_api/library/3.17/init.js"  type = "text/javascript" ></ script >
     < style  type = "text/css" >
         html, body {
             height: 100%;
             width: 100%;
             margin: 0; 
             padding: 0;
         }
         body {
         background-color:#777;
         overflow:hidden;
         font-family: "Trebuchet MS";
         font-size:9pt;
         }
         .dijitAccordionTitle {
             background-color:#777;
         overflow:hidden;
         font-family: "Trebuchet MS";
         font-size:12pt;
         }
         #header {
         background-color:#444;
         color:white;
         font-size:20pt;
         text-align:center;
         font-weight:bold;
         }
         #attributionText {
         background-color:whitesmoke;
         color:black;
         font-size:7pt;
         text-align:left;
         margin:5px;
         }
         #divNeighborhoodName {
         background-color:white;
         color:black;
         font-size:16pt;
         text-align:center;
         font-weight:bold;
         }
         #footer {
         background-color:#444;
         color:yellow;
         font-size:10pt;
         text-align:center;
         }
     </ style >
     < script >
         var map;
         
         require(["dojo/parser",
             "dojo/on",
             "dojo/dom",
             "esri/basemaps",
             "esri/map",
             "esri/geometry/Extent",
             "esri/SpatialReference",
             "esri/layers/ArcGISTiledMapServiceLayer",
             "esri/layers/ArcGISDynamicMapServiceLayer",
             "esri/geometry/Point",
             "esri/tasks/FindTask",
             "esri/tasks/FindParameters",
             "esri/InfoTemplate",
             "esri/symbols/SimpleMarkerSymbol",
             "esri/symbols/SimpleLineSymbol",
             "esri/symbols/SimpleFillSymbol",
             "esri/Color",
             "dijit/layout/BorderContainer",
             "dijit/layout/ContentPane",
             "dojo/domReady!"],
             function (
                 parser,
                 on,
                 dom,
                 esriBasemaps,
                 Map,
                 Extent,
                 SpatialReference,
                 ArcGISTiledMapServiceLayer,
                 ArcGISDynamicMapServiceLayer,
                 Point,
                 FindTask,
                 FindParameters,
                 InfoTemplate,
                 SimpleMarkerSymbol,
                 SimpleLineSymbol,
                 SimpleFillSymbol,
                 Color)
             {
                 parser.parse();
                 esriBasemaps.XjOnline = {
                     baseMapLayers: [{ url: xjuConfig.vectorLayers }],
                     thumbnailUrl: "images/topo.jpg",
                     title: "矢量图"
                 };
                 esriBasemaps.XjRaster = {
                     baseMapLayers: [{ url: xjuConfig.imageLayers }],
                     thumbnailUrl: "images/satellite.jpg",
                     title: "卫星图"
                 };
                 var startExtent = new Extent({
                     "xmin": 73.441277,
                     "ymin": 34.334934,
                     "xmax": 96.388373,
                     "ymax": 49.178574,
                     "spatialReference": { "wkid": 4326 }
                 });
                 //加载底图       
                 map = new Map("mapDiv", {
                     extent: startExtent,
                     zoom: 0,
                     basemap: "XjRaster",
                     sliderStyle: "large",
                     logo: false
                 });
                 // 实例化FindTask        
                 var findTask = new FindTask("http://localhost:6080/arcgis/rest/services/BaseMap/XjOnline/MapServer");
                 // FindTask的参数
                 var findParams = new FindParameters();
                 // 返回Geometry
                 findParams.returnGeometry = true;
                 // 查询的图层id
                 findParams.layerIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36, 37, 46];
                 //findParams.layerIds = [27,28,29,30,31,33,34,35,36,37,46];
                 // 查询字段
                 findParams.searchFields = ["NAME"];
                 var ptSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 1), new Color([0, 255, 0, 0.25]));
                 var lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([255, 0, 0]), 1);
                 on(dom.byId("findBtn"), "click", function () {
                     execute(dom.byId('searchText').value);
                 });
                 // 根据输入的关键字进行findTask操作
                 function execute(searchText) {
                     findParams.searchText = searchText;
                     findTask.execute(findParams, showResults);
                 }
                 // 显示findTask的结果
                 function showResults(results) {
                     // 清除上一次的高亮显示
                     map.graphics.clear();
                     //输出统计结果值
                     dom.byId('totalSearchNum').innerHTML = results.length;
                     var innerHtml = "";
                     var symbol;
                     for (var i = 0; i < results.length; i++) {
                         var curFeature = results[i];
                         var graphic = curFeature.feature;
                         var infoTemplate = null;
                         // 根据类型设置显示样式
                         switch (graphic.geometry.type) {
                             case "point":
                                 symbol = ptSymbol;
                                 infoTemplate = new InfoTemplate("${NAME}", "${*}");
                                 break;
                             case "polyline":
                                 symbol = lineSymbol;
                                 infoTemplate = new InfoTemplate("${NAME}", "${*}");
                                 break;                            
                         }
                         // 设置显示样式
                         graphic.setSymbol(symbol);
                         graphic.setInfoTemplate(infoTemplate);
                         // 添加到graphics进行高亮显示
                         map.graphics.add(graphic);
                         innerHtml += "< a  href=\"javascript:positionFeature(" + graphic.attributes.OBJECTID;
                         if (graphic.attributes.Name) {
                             innerHtml += ",\'" + graphic.attributes.Name + "\')\">" + graphic.attributes.Name + "</ a >< br >";
                         }
                         else {
                             innerHtml += ",\'" + graphic.attributes.NAME + "\')\">" + graphic.attributes.NAME + "</ a >< br >";
                         }                        
                     }
                     dom.byId("contentsContainer").innerHTML = innerHtml;
                     
                 }
                 window.positionFeature = function (id, name) {                    
                     var sGrapphic;
                     //遍历地图的图形查找FID和点击行的FID相同的图形
                     for (var i = 0; i < map.graphics.graphics.length; i++) {
                         var cGrapphic = map.graphics.graphics[i];
                         var sGName = "";
                         if (cGrapphic.attributes.NAME) {
                             sGName = cGrapphic.attributes.NAME;
                         }
                         else {
                             sGName = cGrapphic.attributes.Name;
                         }
                         if ((cGrapphic.attributes) && cGrapphic.attributes.OBJECTID == id && sGName == name) {
                             sGrapphic = cGrapphic;
                             break;
                         }
                     }              
                     // 当点击的名称对应的图形为点类型时进行地图中心定位显示
                     if (sGrapphic.geometry.type == "point") {
                         var cPoint = new Point(sGrapphic.geometry.x, sGrapphic.geometry.y);
                         var p = map.toScreen(cPoint);
                         var iw = map.infoWindow;
                         iw.setTitle(sGrapphic.getTitle());
                         iw.setContent(sGrapphic.getContent());
                         iw.show(p, map.getInfoWindowAnchor(p));
                         map.centerAt(cPoint);
                                               
                     }                        
                     else {                        
                         var cPoint = new Point(sGrapphic.geometry.paths[0][0][0], sGrapphic.geometry.paths[0][0][1]);
                         var p = map.toScreen(cPoint);
                         var iw = map.infoWindow;
                         iw.setTitle(sGrapphic.getTitle());
                         iw.setContent(sGrapphic.getContent());
                         iw.show(p, map.getInfoWindowAnchor(p));
                         map.centerAt(cPoint);
                     }
                 };
             });
     </ script >
</ head >
< body  class = "claro" >
     < div  id = "mainWindow"  data-dojo-type = "dijit/layout/BorderContainer"  data-dojo-props = "design:'headline',gutters:false"  style = "width:100%; height:100%;" >
       <!--标题区域-->
       < div  id = "header"  data-dojo-type = "dijit/layout/ContentPane"  data-dojo-props = "region:'top'"  style = "height:50px;" >
           属性查询图形
       </ div >
       
       <!--中间地图区域-->
       < div  id = "mapDiv"  data-dojo-type = "dijit/layout/ContentPane"  data-dojo-props = "region:'center'"  style = "margin:5px;" >
       </ div >
       <!--右边为属性查询与结果显示区域-->
       < div  data-dojo-type = "dijit/layout/ContentPane"  data-dojo-props = "region:'right'"  style = "width:35%;margin:5px;background-color:whitesmoke;" >     
           输入查询文本
           < input  id = "searchText"  value = "胜利路"  type = "text"  />
           < input  id = "findBtn"  type = "button"  value = "查 找"  />
           < div >查询到的结果数为:< span  id = "totalSearchNum"  style = "font-weight:800;" >0</ span ></ div >
           < div  id = "contentsContainer" ></ div >
       </ div >        
     </ div >
</ body >
</ html >

结果:

wKioL1fUw4rhGobwAAbw2GvW8e8944.jpg




     本文转自stock0991 51CTO博客,原文链接http://blog.51cto.com/qing0991/1851562:,如需转载请自行联系原作者






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值