public class AddTool:IMapServerToolAction
{
public void ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs args)
{
//获取map控件
ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)args.Control;
ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality adfGraphicsMapFunctionality = null;
if (args is PointEventArgs)
{
//转成点
PointEventArgs pointEventArgs = (PointEventArgs)args;
//屏幕点
System.Drawing.Point screenPoint = pointEventArgs.ScreenPoint;
//屏幕坐标转成地理坐标
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));
//MapFunctionality
foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunctionality in adfMap.GetFunctionalities())
{
//当Resource为ADFGraphicsResource,ADFGraphicsResource为GraphicsLayer, 保存在内存中用显示临时图层
if (mapFunctionality.Resource.Name == "GraphicsResource")
{
adfGraphicsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)mapFunctionality;
break;
}
}
//从adfGraphicsMapFunctionality获取名为Element Graphics的DataTable
//ElementGraphicsLayers通常用来显示图形元素,例如显示Map中被选择的图形元素。图层并不用来存储属性,而可以存储不同的图形类型。
ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;
foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
{
if (dataTable.TableName == "Element Graphics")
{
elementGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dataTable;
break;
}
}
//如果名为Element Graphics的DataTable为null,就新建Element Graphics DataTable添加到adfGraphicsMapFunctionality.GraphicsDataSet中,同时刷新Toc1显示
if (elementGraphicsLayer == null)
{
elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
elementGraphicsLayer.TableName = "Element Graphics";
adfGraphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer);
}
//定义标点样式
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
//simpleMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star;
simpleMarkerSymbol.Color = System.Drawing.Color.Green;
simpleMarkerSymbol.Width = 10;
//定义标点选中样式
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol simpleSelectedMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
simpleSelectedMarkerSymbol.Color = System.Drawing.Color.Yellow;
simpleSelectedMarkerSymbol.Width = 12;
simpleSelectedMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star;
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPoint, simpleMarkerSymbol, simpleSelectedMarkerSymbol);
//把标点添加到elementGraphicsLayer
elementGraphicsLayer.Add(graphicElement);
}
else if (args is PolylineEventArgs)
{
PolylineEventArgs lineEventArgs = (PolylineEventArgs)args;
ESRI.ArcGIS.ADF.Web.Geometry.Path pa = new ESRI.ArcGIS.ADF.Web.Geometry.Path();
for (int i = 0; i <= lineEventArgs.Vectors.Length - 1; i++)
{
ESRI.ArcGIS.ADF.Web.Geometry.Point point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(lineEventArgs.Vectors[i].X, lineEventArgs.Vectors[i].Y, adfMap.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
pa.Points.Add(point);
}
ESRI.ArcGIS.ADF.Web.Geometry.Polyline Line = new ESRI.ArcGIS.ADF.Web.Geometry.Polyline();
Line.Paths.Add(pa);
//MapFunctionality
foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunctionality in adfMap.GetFunctionalities())
{
//当Resource为ADFGraphicsResource,ADFGraphicsResource为GraphicsLayer, 保存在内存中用显示临时图层
if (mapFunctionality.Resource.Name == "GraphicsResource")
{
adfGraphicsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)mapFunctionality;
break;
}
}
//从adfGraphicsMapFunctionality获取名为Element Graphics的DataTable
//ElementGraphicsLayers通常用来显示图形元素,例如显示Map中被选择的图形元素。图层并不用来存储属性,而可以存储不同的图形类型。
ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;
foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
{
if (dataTable.TableName == "Element Graphics")
{
elementGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dataTable;
break;
}
}
//如果名为Element Graphics的DataTable为null,就新建Element Graphics DataTable添加到adfGraphicsMapFunctionality.GraphicsDataSet中,同时刷新Toc1显示
if (elementGraphicsLayer == null)
{
elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
elementGraphicsLayer.TableName = "Element Graphics";
adfGraphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer);
}
//定义标点样式
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol simpleMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol();
//simpleMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star;
simpleMarkerSymbol.Color = System.Drawing.Color.Red;
simpleMarkerSymbol.Width = 3;
simpleMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.LineType.Dash;
//定义标点选中样式
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol simpleSelectedMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol();
simpleSelectedMarkerSymbol.Color = System.Drawing.Color.Yellow;
simpleSelectedMarkerSymbol.Width = 3;
//simpleSelectedMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Star;
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(Line, simpleMarkerSymbol, simpleSelectedMarkerSymbol);
//把标点添加到elementGraphicsLayer
elementGraphicsLayer.Add(graphicElement);
}
else if (args is PolygonEventArgs)
{
PolygonEventArgs polygonEventArgs = (PolygonEventArgs)args;
ESRI.ArcGIS.ADF.Web.Geometry.Ring points = new ESRI.ArcGIS.ADF.Web.Geometry.Ring();
for (int i = 0; i <= polygonEventArgs.Vectors.Length - 1; i++)
{
ESRI.ArcGIS.ADF.Web.Geometry.Point point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(polygonEventArgs.Vectors[i].X, polygonEventArgs.Vectors[i].Y, adfMap.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
points.Points.Add(point);
}
ESRI.ArcGIS.ADF.Web.Geometry.Polygon polygon = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
polygon.Rings.Add(points);
//MapFunctionality
foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunctionality in adfMap.GetFunctionalities())
{
//当Resource为ADFGraphicsResource,ADFGraphicsResource为GraphicsLayer, 保存在内存中用显示临时图层
if (mapFunctionality.Resource.Name == "GraphicsResource")
{
adfGraphicsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)mapFunctionality;
break;
}
}
//从adfGraphicsMapFunctionality获取名为Element Graphics的DataTable
//ElementGraphicsLayers通常用来显示图形元素,例如显示Map中被选择的图形元素。图层并不用来存储属性,而可以存储不同的图形类型。
ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;
foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
{
if (dataTable.TableName == "Element Graphics")
{
elementGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dataTable;
break;
}
}
//如果名为Element Graphics的DataTable为null,就新建Element Graphics DataTable添加到adfGraphicsMapFunctionality.GraphicsDataSet中,同时刷新Toc1显示
if (elementGraphicsLayer == null)
{
elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
elementGraphicsLayer.TableName = "Element Graphics";
adfGraphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer);
}
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol simpleMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol();
simpleMarkerSymbol.Color = System.Drawing.Color.Yellow;
simpleMarkerSymbol.FillType= ESRI.ArcGIS.ADF.Web.Display.Symbol.PolygonFillType.DiagCross;
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol simpleSelectedMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol();
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(polygon, simpleMarkerSymbol, simpleSelectedMarkerSymbol);
//把标点添加到elementGraphicsLayer
elementGraphicsLayer.Add(graphicElement);
}
else if(args is RectangleEventArgs)
{
RectangleEventArgs rectargs = (RectangleEventArgs)args;
//矩形
System.Drawing.Rectangle myrect = rectargs.ScreenExtent;
//矩形左下定点坐标转换成地理坐标
ESRI.ArcGIS.ADF.Web.Geometry.Point minpnt = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(myrect.Left, myrect.Bottom, adfMap.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
//矩形右上定点坐标转换成地理坐标
ESRI.ArcGIS.ADF.Web.Geometry.Point maxpnt = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(myrect.Right, myrect.Top, adfMap.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
//
ESRI.ArcGIS.ADF.Web.Geometry.Envelope mappoly = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minpnt, maxpnt);
//MapFunctionality
foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunctionality in adfMap.GetFunctionalities())
{
//当Resource为ADFGraphicsResource,ADFGraphicsResource为GraphicsLayer, 保存在内存中用显示临时图层
if (mapFunctionality.Resource.Name == "GraphicsResource")
{
adfGraphicsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)mapFunctionality;
break;
}
}
//从adfGraphicsMapFunctionality获取名为Element Graphics的DataTable
//ElementGraphicsLayers通常用来显示图形元素,例如显示Map中被选择的图形元素。图层并不用来存储属性,而可以存储不同的图形类型。
ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;
foreach (System.Data.DataTable dataTable in adfGraphicsMapFunctionality.GraphicsDataSet.Tables)
{
if (dataTable.TableName == "Element Graphics")
{
elementGraphicsLayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dataTable;
break;
}
}
//如果名为Element Graphics的DataTable为null,就新建Element Graphics DataTable添加到adfGraphicsMapFunctionality.GraphicsDataSet中,同时刷新Toc1显示
if (elementGraphicsLayer == null)
{
elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
elementGraphicsLayer.TableName = "Element Graphics";
adfGraphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer);
}
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol simpleMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol();
simpleMarkerSymbol.Color = System.Drawing.Color.Yellow;
simpleMarkerSymbol.FillType = ESRI.ArcGIS.ADF.Web.Display.Symbol.PolygonFillType.DiagCross;
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol simpleSelectedMarkerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol();
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(mappoly, simpleMarkerSymbol, simpleSelectedMarkerSymbol);
//把标点添加到elementGraphicsLayer
elementGraphicsLayer.Add(graphicElement);
}
//刷新显示
if (adfMap.ImageBlendingMode == ImageBlendingMode.WebTier)
{
//整个地图控件刷新
adfMap.Refresh();
}
else
{
//只刷新部分Resource
adfMap.RefreshResource(adfGraphicsMapFunctionality.Resource.Name);
}
}
}