ArcGIS.Server.9.2.DotNet 查询地图元素进行定位显示
目的:
1.arcgis server9.2 ADF实现查询结果定位到地图中心显示功能。
准备工作:
1.用ArcGis Server Manager或者ArcCatalog发布一个叫usa的Map Service,并且把这个Service启动起来。
开始:
1.新建名为QueryCenter的ASP.NET Web应用程序,在页面上添加MapResourceManager1、Map1控件,然后做相应的设置把上面发布的usa的Map Service作为MapResourceItem。这些设置比较简单不详细说明,可以参考前面的文章《ArcGIS.Server.9.2.DotNet自带例子分析(一、一)》。
2.然后在页面上添加一个输入框Text1,用来输入查询关键字。添加一个查询按钮Button1,并且给它添加onclick="search()"。添加一个id为list1的div用来显示查询结果。具体的显示看如下的html代码:
Code
3.接下来编写代码,需要实现ICallbackEventHandler的接口,实现GetCallbackResult和RaiseCallbackEvent两个方法。4.在Page_Load生成用户客户端调用的脚本段,具体代码如下:
1public string m_Callback="";
2protected void Page_Load(object sender, EventArgs e)
3{
4//生成客户端脚本段
5 m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", true);
6}
5.把页面切换到html视图,进行脚本事件的编写,首先编写查询按钮Button1的search()事件,代码和说明如下:2protected void Page_Load(object sender, EventArgs e)
3{
4//生成客户端脚本段
5 m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", true);
6}
1function search()
2 {
3 //获取输入的查询关键字
4 var v=document.getElementById("Text1").value;
5 //生成请求字符串
6 var argument = "ControlID=Map1&ControlType=Map&Type=query&EventArg=" + v;
7 var context = "Map";
8 //m_Callback 由服务端的Page_load事件中生成的,用于请求服务端的js代码
9 var script=<%= m_Callback %>
10 //用eval执行字符串
11 eval(script);
12 }
6.接下在RaiseCallbackEvent方法中接收脚本方法search()发起的请求进行处理然后把结果返回给客户端更新页面显示,具体代码如下:2 {
3 //获取输入的查询关键字
4 var v=document.getElementById("Text1").value;
5 //生成请求字符串
6 var argument = "ControlID=Map1&ControlType=Map&Type=query&EventArg=" + v;
7 var context = "Map";
8 //m_Callback 由服务端的Page_load事件中生成的,用于请求服务端的js代码
9 var script=<%= m_Callback %>
10 //用eval执行字符串
11 eval(script);
12 }
1 string v = "";
2 NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
3 if (keyValColl["Type"].ToString() == "query")
4 {
5 System.Text.StringBuilder sb = new System.Text.StringBuilder();
6 string Input = keyValColl["EventArg"];
7 string str = "";
8 string targetlayername = "states";
9 //获取Data Layers的MapFunctionality
10 ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality(0);
11 ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
12 //是否支持QueryFunctionality
13 bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
14 if (supported)
15 {
16 //创建QueryFunctionality
17 ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
18 string[] lids;
19 string[] lnames;
20 //查询图层的id和名称
21 qfunc.GetQueryableLayers(null, out lids, out lnames);
22
23 int layer_index = 0;
24 //获取目标图层的index
25 for (int i = 0; i < lnames.Length; i++)
26 {
27 if (lnames[i] == targetlayername)
28 {
29 layer_index = i;
30 break;
31 }
32 }
33 //空间过滤
34 ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
35 //是否返回地理元素
36 spatialfilter.ReturnADFGeometries = false;
37 //返回最大记录数
38 spatialfilter.MaxRecords = 1000;
39 spatialfilter.Geometry = Map1.GetFullExtent();
40 //查询目标图层并且把结果存入DataTable
41 //查询语句
42 spatialfilter.WhereClause = "STATE_NAME LIKE '" + Input + "%'";
43 System.Data.DataTable datatable = qfunc.Query(null, lids[layer_index], spatialfilter);
44 //组成显示内容
45 for (int i = 0; i < datatable.Rows.Count; i++)
46 {
47 string idv = datatable.Rows[i]["OBJECTID"].ToString();
48 string namev = datatable.Rows[i]["STATE_NAME"].ToString();
49 if (i == 0)
50 {
51 str = "<a href='#' onclick=gocenger('" + idv + "')>" + namev + "</a>";
52 }
53 else
54 {
55 str = str + "<br/>" + "<a href='#' onclick=gocenger('" + idv + "')>" + namev + "</a>";
56 }
57
58 }
59
60
61 }
62
63 //把内容返回给客户端,客户端的processCallbackResult方法会把内容显示在list1中
64 sb.AppendFormat("div:::{0}:::content:::{1}", "list1", str);
65 v = sb.ToString();
66 }
67return v;
7.这样查询功能就完成了,运行然后输入“A”查询地图中STATE_NAME以A为开始的地理元素。2 NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
3 if (keyValColl["Type"].ToString() == "query")
4 {
5 System.Text.StringBuilder sb = new System.Text.StringBuilder();
6 string Input = keyValColl["EventArg"];
7 string str = "";
8 string targetlayername = "states";
9 //获取Data Layers的MapFunctionality
10 ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality(0);
11 ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
12 //是否支持QueryFunctionality
13 bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
14 if (supported)
15 {
16 //创建QueryFunctionality
17 ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
18 string[] lids;
19 string[] lnames;
20 //查询图层的id和名称
21 qfunc.GetQueryableLayers(null, out lids, out lnames);
22
23 int layer_index = 0;
24 //获取目标图层的index
25 for (int i = 0; i < lnames.Length; i++)
26 {
27 if (lnames[i] == targetlayername)
28 {
29 layer_index = i;
30 break;
31 }
32 }
33 //空间过滤
34 ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
35 //是否返回地理元素
36 spatialfilter.ReturnADFGeometries = false;
37 //返回最大记录数
38 spatialfilter.MaxRecords = 1000;
39 spatialfilter.Geometry = Map1.GetFullExtent();
40 //查询目标图层并且把结果存入DataTable
41 //查询语句
42 spatialfilter.WhereClause = "STATE_NAME LIKE '" + Input + "%'";
43 System.Data.DataTable datatable = qfunc.Query(null, lids[layer_index], spatialfilter);
44 //组成显示内容
45 for (int i = 0; i < datatable.Rows.Count; i++)
46 {
47 string idv = datatable.Rows[i]["OBJECTID"].ToString();
48 string namev = datatable.Rows[i]["STATE_NAME"].ToString();
49 if (i == 0)
50 {
51 str = "<a href='#' onclick=gocenger('" + idv + "')>" + namev + "</a>";
52 }
53 else
54 {
55 str = str + "<br/>" + "<a href='#' onclick=gocenger('" + idv + "')>" + namev + "</a>";
56 }
57
58 }
59
60
61 }
62
63 //把内容返回给客户端,客户端的processCallbackResult方法会把内容显示在list1中
64 sb.AppendFormat("div:::{0}:::content:::{1}", "list1", str);
65 v = sb.ToString();
66 }
67return v;
8.查询完成后接下在做一个功能就是点击到查询的名称时把这个查询到的元素作为地图的中心进行显示。在生成上面的查询结果时已经为每个查询到的内容添加了一个gocenger的onclick事件,它有一个参数就是查询到元素的OBJECTID。切换到HTML视图添加gocenger脚本,具体如下:
1function gocenger(id)
2 {
3 //请求字符串
4 var argument = "ControlID=Map1&ControlType=Map&Type=center&EventArg=" + id;
5 var context = "Map";
6 var script=<%= m_Callback %>
7 eval(script);
8 }
9.同样需要在RaiseCallbackEvent方法中对脚本这个方法gocenger发起的请求进行处理,对RaiseCallbackEvent方法进行修改添加,具体代码如下:2 {
3 //请求字符串
4 var argument = "ControlID=Map1&ControlType=Map&Type=center&EventArg=" + id;
5 var context = "Map";
6 var script=<%= m_Callback %>
7 eval(script);
8 }
Code
10.完成了全部功能的开发,运行查看效果。