目的:
1.arcgis server9.2 ADF实现实现点、线、面的MapTips。输入关键字查询出结果,然后给这些结果加上MapTips。
准备工作:
0.参考multimaptips-en.rtf。
1.用ArcGis Server Manager或者ArcCatalog发布一个叫usa的Map Service,并且把这个Service启动起来。
注:要为这个usa的地图添加一个点层名为New_Shapefile,因为maptips控件必须初始设置Layer属性不然会报错,这个控件真是问题多多了。
完成后的效果图:
开始:
1.新建名为myMapTips的ASP.NET Web应用程序,在页面上添加MapResourceManager1、Map1、MapTips1控件、Text1的input(输入查询关键字用)、Button1的input(搜索按钮)。
2.设置MapResourceManager1的属性,这里需要2个MapResourceItem,由上到下分别为maptips、usa。maptips的DataSourceType:GraphicsLayer;usa的DataSourceType:ArcGIS Server Local,就是连接上面发布的usa的Map Service。
3.设置MapTips1控件属性,map="Map1" BodyFormatString="{Id}" HeaderFormatString="{Id}" Layer="MapResourceManager1::usa::New_Shapefile" WhereClause="2>1" 上面的这些属性必须设置不然会报错了,这个控件设计的真是怪,还有一点了要在程序里动态设置MapTips1控件属性并且刷新必须设置 ShowOnlyWhenLayerIsVisible="False"不然也会报错。
4.Button1的input(搜索按钮),添加onclick事件:search(),其他的控件也做好相应的设置。
5.接下开始代码编写,首先实现ICallbackEventHandler接口,实现GetCallbackResult()和RaiseCallbackEvent(string eventArgument)两方法,具体代码如下:
2 {
3 public string m_Callback = "";
4 protected void Page_Load(object sender, EventArgs e)
5 {
6 m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", "processCallbackError", true);
7 }
8
9 ICallbackEventHandler 成员#region ICallbackEventHandler 成员
10
11 private string _callbackArg;
12 string ICallbackEventHandler.GetCallbackResult()
13 {
14 return RaiseCallbackEvent(_callbackArg);
15 }
16
17 void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
18 {
19 _callbackArg = eventArgument;
20 }
21
22 #endregion
23
24 private string RaiseCallbackEvent(string _callbackArg)
25 {
26 return "";
27 }
2 // 搜索事件
3 function search()
4 {
5 var v=document.getElementById("Text1").value;
6 var argument = "ControlID=Map1&ControlType=Map&Type=query&EventArg=" + v;
7 var context = "Map";
8 var rv=<%= m_Callback %>;
9 eval(rv);
10
11 }
12
13 function processCallbackError()
14 {
15 alert("错误啦!");
16 }
17 < / script>
8.接下来切换cs代码端处理 search()的脚本方法发起的请求,具体在RaiseCallbackEvent(string _callbackArg)方法中进行请求处理并且把处理结果返回给客户端,具体代码和说明如下:
2 {
3 CallbackResultCollection m_ADFCallbackResultCollection = new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection();
4 string v = "";
5 NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
6 if (keyValColl["Type"].ToString() == "query")
7 {
8 //查询结果数
9 int featurecount = 0;
10 //获取usa的MapFunctionality、Resource
11 ESRI.ArcGIS.ADF.Web.UI.WebControls.Map map = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)Map1;
12 ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)map.GetFunctionality("usa");
13 ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gr = mf.Resource;
14 ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qf;
15 string[] lids;
16 string[] lnames;
17 //创建QueryFunctionality
18 qf = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gr.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
19 //查询图层id、名称
20 qf.GetQueryableLayers(null, out lids, out lnames);
21
22 //获取maptips的MapFunctionality、Resource
23 ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf_maptips = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)map.GetFunctionality("maptips");
24 ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gr_matips = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)mf_maptips.Resource;
25 //创建FeatureGraphicsLayer
26 ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer gl_maptips = new ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer("maptips", ESRI.ArcGIS.ADF.Web.FeatureType.Point, new ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer(ESRI.ArcGIS.ADF.Web.FeatureType.Point, System.Drawing.Color.Red));
27
28 //给新建的FeatureGraphicsLayer添加字段
29 gl_maptips.Columns.Add("peid", typeof(Int32));
30 gl_maptips.Columns.Add("objectid", typeof(Int32));
31 gl_maptips.Columns.Add("name", typeof(string));
32 gr_matips.Graphics.Tables.Clear();
33
34 IMapFunctionality pMapfun;
35 DataTable dt4 = null;
36 ESRI.ArcGIS.ADF.Web.SpatialFilter sfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
37 //获取查询关键字
38 string Input = keyValColl["EventArg"];
39 for (int i = 0; i < lids.Length; i++)
40 {
41 //设置过滤条件
42 sfilter.ReturnADFGeometries = true;
43 sfilter.MaxRecords = 100;
44 sfilter.Geometry = null;
45 sfilter.WhereClause = "NAME LIKE '" + Input + "%'";
46 //查询并且把结果放在dt4中
47 dt4 = qf.Query(null, lids[i], sfilter);
48 if (dt4 != null)
49 {
50 //累加查询到的结果数
51 featurecount += dt4.Rows.Count;
52
53 //对查询到结果进行遍历,更加不同的类型在FeatureGraphicsLayer中生成显示点
54 foreach (DataRow dr in dt4.Rows)
55 {
56 int objectid = Int32.Parse(dr["objectid"].ToString());
57 int layerid = Int32.Parse(lids[i]);
58 pMapfun = mf;
59 if (pMapfun.Resource is ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
60 {
61 ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal pLocal = pMapfun.Resource as ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal;
62 ESRI.ArcGIS.Carto.IMapServer pMapServer = (ESRI.ArcGIS.Carto.IMapServer)pLocal.MapServer;
63 //获取Objects
64 ESRI.ArcGIS.Carto.IMapServerObjects pMapServerObjects = pMapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
65 //用Objects的方法获取FeatureLayer
66 ESRI.ArcGIS.Carto.IFeatureLayer pFLayer = pMapServerObjects.get_Layer(pMapServer.get_MapName(0), layerid) as ESRI.ArcGIS.Carto.IFeatureLayer;
67 ESRI.ArcGIS.Geodatabase.IFeatureClass pFClass = pFLayer.FeatureClass;
68 //根据objectid获取Feature
69 ESRI.ArcGIS.Geodatabase.IFeature pFeature = pFClass.GetFeature(objectid);
70 //获取Geometry
71 ESRI.ArcGIS.Geometry.IGeometry pGeometry = pFeature.Shape;
72 //查询结果为点的时候
73 if (pGeometry.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
74 {
75 //新建点并且添加到FeatureGraphicsLayer
76 IPoint ippoint = pGeometry as IPoint;
77 ESRI.ArcGIS.ADF.Web.Geometry.Point adfpt = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPoint(ippoint);
78 DataRow dtr = gl_maptips.Add(adfpt);
79 //添加属性值
80 try
81 {
82 dtr["objectid"] = objectid;
83 dtr["peid"] = dr["peid"];
84 dtr["name"] = dr["name"];
85 }
86 catch
87 {
88 dtr["objectid"] = objectid;
89 dtr["peid"] = 0;
90 dtr["name"] = dr["name"];
91 }
92 }
93 //查询结果为线的时候
94 if (pGeometry.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
95 {
96 IPolyline ippolyline = pGeometry as IPolyline;
97 ESRI.ArcGIS.Geometry.IPointCollection com_pointcollection = (ESRI.ArcGIS.Geometry.IPointCollection)ippolyline;
98 ESRI.ArcGIS.ADF.Web.Geometry.Point[] new_adf_points = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPointCollection(com_pointcollection);
99 ESRI.ArcGIS.ADF.Web.Geometry.Point adfpt = new ESRI.ArcGIS.ADF.Web.Geometry.Point();
100 //获取线的中间一个点
101 adfpt = new_adf_points[new_adf_points.Length / 2];
102 DataRow dtr = gl_maptips.Add(adfpt);
103 try
104 {
105 dtr["objectid"] = objectid;
106 dtr["peid"] = dr["peid"];
107 dtr["name"] = dr["name"];
108 }
109 catch
110 {
111 dtr["objectid"] = objectid;
112 dtr["peid"] = 0;
113 dtr["name"] = dr["name"];
114 }
115
116 }
117 //查询结果为面的时候
118 if (pGeometry.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
119 {
120 IPolygon ippolygon = pGeometry as IPolygon;
121 //面曲线的开始点
122 ESRI.ArcGIS.Geometry.IPoint ippoint = ippolygon.FromPoint;
123 ESRI.ArcGIS.ADF.Web.Geometry.Point adfpt = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPoint(ippoint);
124 DataRow dtr = gl_maptips.Add(adfpt);
125 try
126 {
127 dtr["objectid"] = objectid;
128 dtr["peid"] = dr["peid"];
129 dtr["name"] = dr["NAME"];
130 }
131 catch
132 {
133 dtr["objectid"] = objectid;
134 dtr["peid"] = 0;
135 dtr["name"] = dr["NAME"];
136 }
137
138 }
139 }//end if
140 }//end foreach
141 }
142
143 }// end for
144 //把FeatureGraphicsLayer添加到maptips
145 gr_matips.Graphics.Tables.Add(gl_maptips);
146 if (featurecount > 0)
147 {
148 //设置地图视图范围
149 ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom;
150 ESRI.ArcGIS.ADF.Web.Geometry.Envelope env = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(double.MaxValue, double.MaxValue, double.MinValue, double.MinValue);
151 foreach (DataRow row in gl_maptips.Rows)
152 {
153 geom = gl_maptips.GeometryFromRow(row);
154 env.Union(geom);
155 }
156 int percent = 20;
157 double newwidth, newheight;
158 newwidth = map.GetFullExtent().Width * (percent / 100);
159 newheight = map.GetFullExtent().Height * (percent / 100);
160 env.XMin -= newwidth;
161 env.XMax += newwidth;
162 env.YMin -= newheight;
163 env.YMax += newheight;
164 map.Extent = env;
165
166 //设置MapTips1的属性
167 MapTips1.FeatureLimit = 20;
168 MapTips1.Layer = "MapResourceManager1::maptips::maptips";
169 MapResourceManager mr = MapResourceManager1;
170 MapTips1.HeaderFormatString = "{objectid}";
171 MapTips1.BodyFormatString = "{name}";
172 MapTips1.Features = null;
173 MapTips1.WhereClause = "2>1";
174 MapTips1.Map = map.ClientID;
175 //刷新MapTips1
176 MapTips1.Refresh();
177
178 m_ADFCallbackResultCollection.Add(MapTips1.CallbackResults[0]);
179 m_ADFCallbackResultCollection.Add(Map1.CallbackResults[0]);
180 }
181 }
182
183 return m_ADFCallbackResultCollection.ToString();
184 }