大家好,欢迎来到IT知识分享网。
ArcGIS二次开发基础教程(08):在MapControl上画图(添加元素)
添加临时元素
0. 点元素
//临时画图的元素将不会保存在地图中
//全局变量
IPoint pt;
IElement ele;
public void drawPoint(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
pt = new PointClass();
//获取单击位置地图坐标
pt.PutCoords(e.mapX,e.mapY);
//创建点要素
ele = new MarkElementClass();
//设置点要素的空间几何体为点
ele.Geometry = pt as IGeometry;
//图形容器
IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
//添加元素
graphicsConainer.AddElement(ele,0);
axMapControl1.Refresh();
}
//菜单栏“点要素” 选项 添加事件开启画图状态
private void 点要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
axMapControl1.OnMouseDown += drawPoint;
}
1. 线元素
//全局变量
//点集
IPointCollection ptCollection;
IPoint linePt;
IElement ele;
public void drawLine(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
linePt = new PointClass();
linePt.PutCoords(e.mapX,e.mapY);
ptCollection.AddPoint(linePt);
//多段线要素
ele = new PolylineElementClass();
IPolyline polyline = new PolylineClass();
//将点集转换为多段线
polyline = ptCollection as IPolyline;
ele.Geometry = polyline as IGeometry;
//图形容器
IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
//添加元素
graphicsConainer.AddElement(ele,0);
axMapControl1.Refresh();
}
private void 线要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
//每次开启画图状态都重新创建点集
ptCollection = new PolylineClass();
axMapControl1.OnMouseDown += drawLine;
}
2. 面元素
//全局变量
IPoint polygonPt;
IElement ele;
IPointCollection ptCollection;
public void drawPolygon(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
polygonPt = new PointClass();
polygonPt.PutCoords(e.mapX,e.mapY);
ptCollection.AddPoint(polyginPt);
ele = new PolygonElementClass();
IPolygon polygon = new PolygonClass();
polygon = ptCollection as IPolygon;
ele.Geometry = polygon as IGeometry;
//图形容器
IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
//添加元素
graphicsConainer.AddElement(ele,0);
axMapControl1.Refresh();
}
private void 面要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
polygonCollect = new PolygonClass();
axMapControl1.OnMouseDown += drawPolygon;
}
添加元素到要素中
0. 点元素
//此处添加的元素将保存在地图中
//全局变量
IPoint pt;
public void vectorPoint(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
//获取图层及要素类
IFeatureLayer featureLayer = GetLayerByName("图层名称");
IFeatureClass featureClass = featureLayer.FeatrueClass;
//创建点要素并赋值坐标信息
pt = new PointClass();
pt.PutCoords(e.mapX,e.mapY);
//从要素类创建新要素
IFeature feature = featureClass.CreateFeature();
//将点元素设置为新建要素的Shape
feature.Shape = pt as IGeomerty;
//保存
feature.Store();
axMapControl1.Refresh();
}
//菜单栏“矢量点” 选项 添加事件开启画图状态
private void 矢量点ToolStripMenuItem_Click(object sender, EventArgs e)
{
axMapControl1.OnMouseDown += vectorPoint;
}
1. 线元素
//全局变量
IPoint linePt;
IPointCollection ptCollection;
public void vectorLine(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
//获取图层及要素类
IFeatureLayer featureLayer = GetLayerByName("图层名称");
IFeatureClass featureClass = featureLayer.FeatrueClass;
linePt = new PointClass();
linePt.PutCoords(e.mapX,e.mapY);
ptCollection.AddPoint(linePt);
IPolyline polyline = new PolylineClass();
polyline = ptCollection as IPolyline;
IFeature feature = featureClass.CreateFeature();
feature.Shape = polyline as IGeometry;
feature.Store();
axMapControl1.Refresh();
}
private void 矢量线ToolStripMenuItem_Click(object sender, EventArgs e)
{
pointCollect = new PolylineClass();
axMapControl2.OnMouseDown += vectorLine;
}
2. 面元素
//全局要素
IPoint pt;
IPointCollection ptCollection;
public void vectorPolygon(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
//获取图层及要素类
IFeatureLayer featureLayer = GetLayerByName("图层名称");
IFeatureClass featureClass = featureLayer.FeatrueClass;
pt = new PointClass();
pt.PutCoords(e.mapX,e.MapY);
ptCollection.AddPoint(pt);
IPolygon polygon = new PolygonClass();
polygon = ptCollection as IPolygon;
IFeature feature = featureClass.CreateFeature();
feature.Shape = polygon as IGeometry;
feature.Store();
axMapControl1.Refresh();
}
private void 矢量面ToolStripMenuItem_Click(object sender, EventArgs e)
{
polygonCollect = new PolygonClass();
axMapControl2.OnMouseDown += vectorPolygon;
}
3. 文本元素
//实现为区县地图的每个区县添加区县名
IFeatureLayer layer = GetLayerByName("图层名称")
IFeatureCursor cursor = layer.FeatureClass.Search(null,true);
IFeature feature = cursor.NextFeature();
IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
while(feature!=null)
{
//获取区县名
string str = feature.get_Value(feature.Fields.FindField("区县名")).ToString();
//创建一个文本元素
TextElement textElement = new TextElementClass();
//字体
IFontDisp font = new StdFontClass() as IFontDisp;
font.Bold = true;
font.Size = 10;
font.Name = "宋体";
//文本颜色
IRgbColor color = new RgbColorClass();
color.Red = 0;
color.Green = 0;
color.Blue = 0;
ITextSymbol textSymbol = new TextSymbolClass();
textSymbol.Font = font;
textSymbol.Color = color;
textElement.Symbol = textSymbol;
textElement.Text = str;
IElement element = textElement as IElement;
//文本显示位置在区县要素中心
IPoint point = new PointClass();
IGeometry5 geo = feature.Shape as IGeometry5;
point.X = geo.CentroidEx.X;
point.Y = geo.CentroidEx.Y;
element.Geometry = point as IGeometry;
graphicsContainer.AddElement(element,0);
feature = cursor.NextFeature();
} axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/24794.html