目的:
1.arcgis server9.2 ADF的AddGraphics。
准备工作:
1.用ArcGis Server Manager或者ArcCatalog发布一个叫world的Map Service,并且把这个Service启动起来。
2.找到DeveloperKit\SamplesNET\Server\Web_Applications目录下的Common_AddGraphicsCSharp.zip。
开始:
1.新建名为AddGraphics的ASP.NET Web应用程序。
2.在页面上放置1个Map、1个Toc、1个MapResourceManager、1个Toolbar控件,做好相应的设置比较简单也不详细说了,也可以参考前面的几篇文章。
3.主要说一下MapResourceManager1设置了,这次与前几篇不同了用的是ArcGIS Server Internet,用编辑器添加以一个名为ServerResource的MapResourceItem,然后点击Definition属性做如下设置,Type:ArcGIS Server Internet;Data Source:http://机器名/arcgis/services;Identity:输入机器的账号和密码;Resource:(default)@world。
4.在Toolbar1添加一个Tool,Name:AddPointTool;Text:Add Graphic Point;ClientAction:Point;ServerActionAssembly:AddGraphics;ServerActionClass:AddGraphics.ElementGraphicTool。
5.新建GraphicPointTools.cs文件,然后在文件中添加ElementGraphicTool类用来实现AddPointTool的功能,代码和说明如下:
2 {
3 void IMapServerToolAction.ServerAction(ToolEventArgs toolEventArgs)
4 {
5 //获取map控件
6 ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap =(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control;
7 //转成点
8 PointEventArgs pointEventArgs = (PointEventArgs)toolEventArgs;
9 //屏幕点
10 System.Drawing.Point screenPoint = pointEventArgs.ScreenPoint;
11
12 //屏幕坐标转成地理坐标
13 ESRI.ArcGIS.ADF.Web.Geometry.Point adfPoint =ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screenPoint.X, screenPoint.Y, adfMap.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
14
15 ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality adfGraphicsMapFunctionality = null;
16 //MapFunctionality
17 foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunctionality in adfMap.GetFunctionalities())
18 {
19 //当Resource为ADFGraphicsResource,ADFGraphicsResource为GraphicsLayer, 保存在内存中用显示临时图层
20 if (mapFunctionality.Resource.Name == "ADFGraphicsResource")
21 {
22 adfGraphicsMapFunctionality =(ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)mapFunctionality;
23 break;
24 }
25 }
26
27 //当为null的时候调用Utility.ProcessError方法弹出提示框
28 if (adfGraphicsMapFunctionality == null)
29 {
30 //把Utility.ProcessError处理的CallbackResultCollection结果赋给Map控件
31 adfMap.CallbackResults.CopyFrom(Utility.ProcessError("ADF graphics functionality not found"));
32 return;
33 }
34
35 //从adfGraphicsMapFunctionality获取名为Element Graphics的DataTable
36 ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;
37 foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
38 {
39 if (dataTable.TableName == "Element Graphics")
40 {
41 elementGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dataTable;
42 break;
43 }
44 }
45
46 //如果名为Element Graphics的DataTable为null,就新建Element Graphics DataTable添加到adfGraphicsMapFunctionality.GraphicsDataSet中,同时刷新Toc1显示
47 if (elementGraphicsLayer == null)
48 {
49 elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
50 elementGraphicsLayer.TableName = "Element Graphics";
51 adfGraphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer);
52
53 //查找Toc1控件
54 Toc adfToc = (Toc)Utility.FindControl("Toc1", adfMap.Page);
55 //刷新Toc1控件
56 adfToc.Refresh();
57 //CallbackResults结果赋给Map控件
58 adfMap.CallbackResults.CopyFrom(adfToc.CallbackResults);
59 }
60
61 //定义标点样式
62 ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol simpleMarkerSymbol =new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
63 simpleMarkerSymbol.Color = System.Drawing.Color.Black;
64 simpleMarkerSymbol.Width = 10;
65
66 //定义标点选中样式
67 ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol simpleSelectedMarkerSymbol =new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
68 simpleSelectedMarkerSymbol.Color = System.Drawing.Color.Yellow;
69 simpleSelectedMarkerSymbol.Width = 12;
70 simpleSelectedMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star;
71
72 ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement =new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPoint, simpleMarkerSymbol, simpleSelectedMarkerSymbol);
73 //把标点添加到elementGraphicsLayer
74 elementGraphicsLayer.Add(graphicElement);
75 //刷新显示
76 if (adfMap.ImageBlendingMode == ImageBlendingMode.WebTier)
77 {
78 //整个地图控件刷新
79 adfMap.Refresh();
80 }
81 else
82 {
83 //只刷新部分Resource
84 adfMap.RefreshResource(adfGraphicsMapFunctionality.Resource.Name);
85 }
86 }
87 }
2{
3 public class Utility
4 {
5 //错误信息处理
6 public static ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection ProcessError(string message)
7 {
8 //alert错误信息
9 string jsAlertException = "alert('" + message + "')";
10 //用CallbackResult执行js脚本
11 ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult alertCallbackResult =new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(null, null, "javascript", jsAlertException);
12
13 //设置鼠标的指针显示
14 string jsChangeCursor = "map.divObject.style.cursor = map.cursor";
15 //用CallbackResult执行js脚本
16 ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult cursorCallbackResult =new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(null, null, "javascript", jsChangeCursor);
17
18 ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection callbackResultCollection =new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection();
19 callbackResultCollection.Add(alertCallbackResult);
20 callbackResultCollection.Add(cursorCallbackResult);
21
22 //返回CallbackResultCollection
23 return callbackResultCollection;
24 }
25
26 //查找页面控件
27 public static Control FindControl(string control, Page page)
28 {
29 if (page == null || control == null)
30 {
31 return null;
32 }
33 Control buddyControl = page.FindControl(control);
34 if (buddyControl == null)
35 {
36 string webPartControl = GetControlUniqueID(control, page.Controls);
37 if (webPartControl != null)
38 {
39 buddyControl = page.FindControl(webPartControl);
40 }
41 else
42 {
43 buddyControl = page.FindControl(control);
44 }
45
46 }
47 return buddyControl;
48 }
49
50 public static string GetControlUniqueID(string controlID, ControlCollection controls)
51 {
52 Control control;
53 string uniqueID = null;
54 for (int i = 0; i < controls.Count; ++i)
55 {
56 control = controls[i];
57 if (control.ID == controlID)
58 {
59 uniqueID = control.UniqueID;
60 break;
61 }
62 if (control.Controls.Count > 0)
63 {
64 uniqueID = GetControlUniqueID(controlID, control.Controls);
65 if (uniqueID != null)
66 break;
67 }
68 }
69 return uniqueID;
70 }
71
72 }
73}
74