ArcGIS.Server.9.2.DotNet自带例子分析(三、二)

目的:
1.arcgis server9.2 ADF的AddGraphics-FeatureGraphicLayer

准备工作:
1.(三、一)的工程,具体见前篇。

开始: 
1.在页面上在新增一个Toolbar2,在ToolbarItems中添加一个Tool然后设置相应的属性,具体如下:

1<ToolbarItems>
2<esri:Tool ClientAction="Point" JavaScriptFile="" Name="AddFeature" ServerActionAssembly="AddGraphics" ServerActionClass="AddGraphics.FeatureGraphicTool"Text="Add Feature Point" />
3</ToolbarItems>

2.在GraphicPointTools.cs中新增加FeatureGraphicTool类来实现上面的Tool的功能,具体代码和说明如下:

  1public class FeatureGraphicTool : IMapServerToolAction
  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            //FeatureGraphicLayer类似feature layer,能建立属性信息,并能基于属性来符号化。图层只能存储同一图形类型。FeatureGraphicLayer可以支持查询。
 36            ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer = null;
 37            //从adfGraphicsMapFunctionality获取名为Feature Graphics的DataTable
 38            foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
 39            {
 40                if (dataTable.TableName == "Feature Graphics")
 41                {
 42                    featureGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer)dataTable;
 43                    break;
 44                }

 45            }

 46
 47            //如果名为Feature Graphics的DataTable为null,就新建Feature Graphics DataTable
 48            if (featureGraphicsLayer == null)
 49            {
 50                //新建DataTable Feature Graphics
 51                featureGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer();
 52                featureGraphicsLayer.TableName = "Feature Graphics";
 53                adfGraphicsMapFunctionality.GraphicsDataSet.Tables.Add(featureGraphicsLayer);
 54
 55                //添加字段x
 56                System.Data.DataColumn xDataColumn =new System.Data.DataColumn("X", System.Type.GetType("System.Double"));
 57                featureGraphicsLayer.Columns.Add(xDataColumn);
 58                //添加字段y
 59                System.Data.DataColumn yDataColumn =new System.Data.DataColumn("Y", System.Type.GetType("System.Double"));
 60                featureGraphicsLayer.Columns.Add(yDataColumn);
 61                //添加字段CustomDataColumn
 62                System.Data.DataColumn customDataColumn =new System.Data.DataColumn("CustomDataColumn",System.Type.GetType("System.Int32"));
 63                featureGraphicsLayer.Columns.Add(customDataColumn);
 64
 65                //CustomDataColumn的值等于0时的显示样式
 66                ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int> zeroUniqueValue =new ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int>(0);
 67                ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol zeroSymbol =new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
 68                zeroSymbol.Color = System.Drawing.Color.Red;
 69                zeroSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Circle;
 70                zeroSymbol.Width = 12;
 71                zeroUniqueValue.Symbol = zeroSymbol;
 72                zeroUniqueValue.SymbolLabel = "0";
 73
 74                //CustomDataColumn的值等于1时的显示样式
 75                ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int> oneUniqueValue =new ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int>(1);
 76                ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol oneSymbol =new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
 77                oneSymbol.Color = System.Drawing.Color.Blue;
 78                oneSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Circle;
 79                oneSymbol.Width = 12;
 80                oneUniqueValue.Symbol = oneSymbol;
 81                oneUniqueValue.SymbolLabel = "1";
 82
 83        
 84                ESRI.ArcGIS.ADF.Web.Display.Renderer.ValueMapRenderer<int> valueMapRenderer =new ESRI.ArcGIS.ADF.Web.Display.Renderer.ValueMapRenderer<int>();
 85                valueMapRenderer.ValueColumnName = "CustomDataColumn";
 86
 87                //默认的显示样式
 88                valueMapRenderer.DefaultLabel = "Default Value";
 89                ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol defaultSymbol =new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
 90                defaultSymbol.Color = System.Drawing.Color.Black;
 91                defaultSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Cross;
 92                defaultSymbol.Width = 12;
 93                valueMapRenderer.DefaultSymbol = defaultSymbol;
 94
 95                ESRI.ArcGIS.ADF.Web.Display.Renderer.ValueCollection<int> valueCollection =valueMapRenderer.Values;
 96                valueCollection.Add(zeroUniqueValue);
 97                valueCollection.Add(oneUniqueValue);
 98
 99                //Renderer数据显示样式
100                featureGraphicsLayer.Renderer = valueMapRenderer;
101
102                //刷新Toc1
103                Toc adfToc = (Toc)Utility.FindControl("Toc1", adfMap.Page);
104                adfToc.Refresh();
105                //通知客户端更新显示
106                adfMap.CallbackResults.CopyFrom(adfToc.CallbackResults);
107
108                // 执行客户端脚本
109                CallbackResult enableCheckBoxCallbackResult =new CallbackResult(nullnull"javascript"string.Format("document.getElementById('{0}').disabled = false""maptipsCheckBox"));
110                //通知客户端更新显示
111                adfMap.CallbackResults.Add(enableCheckBoxCallbackResult);
112
113            }

114
115            System.Data.DataRow newDataRow = featureGraphicsLayer.Add(adfPoint);
116            //随机的给CustomDataColumn赋值,随机的出现上面定义的2个显示样式
117            Random randomizer = new Random();
118            int randomValue = randomizer.Next(02);
119            newDataRow["CustomDataColumn"= randomValue;
120
121            //把x,y存到表里
122            newDataRow["X"= string.Format("{0:#.##}", adfPoint.X);
123            newDataRow["Y"= string.Format("{0:#.##}", adfPoint.Y);
124
125            //刷新地图显示
126            if (adfMap.ImageBlendingMode == ImageBlendingMode.WebTier)
127            
128                adfMap.Refresh(); 
129            }

130            else
131            
132                adfMap.RefreshResource(adfGraphicsMapFunctionality.Resource.Name); 
133            }

134
135        }

136    }
3.在Toolbar2的ToolbarItems中在添加一个DropDownBox然后设置相应的属性,具体如下:

1<esri:DropDownBox ClientAction="" DropDownListWidth="100" Items="&lt;Items&gt;&lt;ListItem Text=&quot;Red_Blue&quot; Value=&quot;Red_Blue&quot; Selected=&quot;True&quot; /&gt;&lt;ListItem Text=&quot;Yellow_Green&quot; Value=&quot;Yellow_Green&quot; Selected=&quot;False&quot; /&gt;&lt;ListItem Text=&quot;Orange_Purple&quot; Value=&quot;Orange_Purple&quot; Selected=&quot;False&quot; /&gt;&lt;/Items&gt;"
2                          JavaScriptFile="" Label="Colors:" LabelWidth="100" Name="DropDownBoxColor" SelectedIndex="0"
3                          ServerActionAssembly="AddGraphics" ServerActionClass="AddGraphics.FeatureDropDownColor" />

4.在GraphicPointTools.cs中新增加FeatureDropDownColor类来实现上面的DropDownBox的功能,具体代码和说明如下:

 1public class FeatureDropDownColor : IMapServerDropDownBoxAction
 2    {
 3        void IServerAction.ServerAction(ToolbarItemInfo toolbarItemInfo)
 4        {
 5            //获取地图控件
 6            ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap =(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolbarItemInfo.BuddyControls[0];
 7            //获取DropDownBoxColor控件
 8            DropDownBox adfDropDownBox =(DropDownBox)toolbarItemInfo.Toolbar.ToolbarItems.Find("DropDownBoxColor");
 9            //获取DropDownBoxColor的选择值
10            string selectedColor = adfDropDownBox.SelectedValue;
11            //MapFunctionality
12            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality adfGraphicsMapFunctionality = null;
13            foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunctionality in adfMap.GetFunctionalities())
14            {
15                if (mapFunctionality.Resource.Name == "ADFGraphicsResource")
16                {
17                    adfGraphicsMapFunctionality =(ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)mapFunctionality;
18                    break;
19                }

20            }

21            //当为null的时候调用Utility.ProcessError方法弹出提示框
22            if (adfGraphicsMapFunctionality == null)
23            {
24                adfMap.CallbackResults.CopyFrom(Utility.ProcessError("ADF graphics functionality not found"));
25                return;
26            }

27
28            //FeatureGraphicLayer类似feature layer,能建立属性信息,并能基于属性来符号化。图层只能存储同一图形类型。FeatureGraphicLayer可以支持查询。
29            ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer = null;
30            //从adfGraphicsMapFunctionality获取名为Feature Graphics的DataTable
31            foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
32            {
33                if (dataTable.TableName == "Feature Graphics")
34                {
35                    featureGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer)dataTable;
36                    break;
37                }

38            }

39
40            //如果Feature Graphics的DataTable为null,设置DropDownBoxColor为原始值,同时调用Utility.ProcessError方法弹出提示框
41            if (featureGraphicsLayer == null)
42            {
43                //设置DropDownBoxColor为原始值的脚本
44                string jsResetDropDownList = string.Format("document.getElementById('{0}{1}{2}').value = 'Red_Blue'",toolbarItemInfo.Toolbar.ClientID, adfDropDownBox.Name, adfDropDownBox.Type);
45                //执行上面的脚本
46                CallbackResult resetDropDownListCallbackResult =new CallbackResult(nullnull"javascript", jsResetDropDownList);
47                adfMap.CallbackResults.Add(resetDropDownListCallbackResult);
48                //调用Utility.ProcessError方法弹出提示框
49                adfMap.CallbackResults.CopyFrom(Utility.ProcessError("ADF feature graphics layer not found"));
50                return;
51            }

52
53            //获取当前的Renderer
54            ESRI.ArcGIS.ADF.Web.Display.Renderer.ValueMapRenderer<int> currentValueMapRenderer =(ESRI.ArcGIS.ADF.Web.Display.Renderer.ValueMapRenderer<int>)featureGraphicsLayer.Renderer;
55            //获取当前的ValueCollection
56            ESRI.ArcGIS.ADF.Web.Display.Renderer.ValueCollection<int> currentValueCollection =currentValueMapRenderer.Values;
57
58            ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int> currentZeroUniqueValue =(ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int>)currentValueCollection[0];
59            ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int> currentOneUniqueValue =(ESRI.ArcGIS.ADF.Web.Display.Renderer.UniqueValue<int>)currentValueCollection[1];
60
61            ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol currentZeroSymbol =(ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol)currentZeroUniqueValue.Symbol;
62            ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol currentOneSymbol =(ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol)currentOneUniqueValue.Symbol;
63
64            //更加不同的颜色设置不同的显示颜色
65            switch (selectedColor)
66            {
67                case "Red_Blue":
68                    currentZeroSymbol.Color = System.Drawing.Color.Red;
69                    currentOneSymbol.Color = System.Drawing.Color.Blue;
70                    break;
71                case "Yellow_Green":
72                    currentZeroSymbol.Color = System.Drawing.Color.Yellow;
73                    currentOneSymbol.Color = System.Drawing.Color.Green;
74                    break;
75                case "Orange_Purple":
76                    currentZeroSymbol.Color = System.Drawing.Color.Orange;
77                    currentOneSymbol.Color = System.Drawing.Color.Purple;
78                    break;
79                default:
80                    break;
81            }

82
83            //刷新Toc1显示
84            Toc adfToc = (Toc)Utility.FindControl("Toc1", adfMap.Page);
85            adfToc.Refresh();
86            adfMap.CallbackResults.CopyFrom(adfToc.CallbackResults);
87
88            //刷新地图显示
89            if (adfMap.ImageBlendingMode == ImageBlendingMode.WebTier)
90            
91                adfMap.Refresh(); 
92            }

93            else
94            
95                adfMap.RefreshResource(adfGraphicsMapFunctionality.Resource.Name); 
96            }

97        }

98    }

5.这样就完成了添加上的点的颜色变更的功能,然后在Toolbar2的ToolbarItems中在添加一个DropDownBox然后设置相应的属性用来实现变换添加点的显示形状的功能,具体如下:

1<esri:DropDownBox ClientAction="" DropDownListWidth="100" Items="&lt;Items&gt;&lt;ListItem Text=&quot;Circle&quot; Value=&quot;Circle&quot; Selected=&quot;True&quot; /&gt;&lt;ListItem Text=&quot;Square&quot; Value=&quot;Square&quot; Selected=&quot;False&quot; /&gt;&lt;ListItem Text=&quot;Star&quot; Value=&quot;Star&quot; Selected=&quot;False&quot; /&gt;&lt;/Items&gt;"
2                          JavaScriptFile="" Label="Type:" LabelWidth="100" Name="DropDownBoxSymbol" SelectedIndex="0"
3                          ServerActionAssembly="AddGraphics" ServerActionClass="AddGraphics.FeatureDropDownSymbol" />
6.在GraphicPointTools.cs中新增加FeatureDropDownSymbol类来实现上面的DropDownBoxSymbol的功能,具体代码和说明如下:
 1public class FeatureDropDownSymbol : IMapServerDropDownBoxAction
 2    {
 3        IServerAction Members
96
97    }
7.这样就完成了添加点的形状变换的功能了。本例子到这里也分析结束了,关于其中的MapTips 的功能好像有问题了这里就暂时不讲了。
posted @ 2008-08-19 22:10  水的右边  阅读(3239)  评论(0编辑  收藏  举报