ArcGIS Engine二次开发VS2013使用MuneStirp控件实现两点距离测量
时间: 2023-07-11 09:04:23 浏览: 114
下面是实现两点距离测量的步骤:
1. 在 ArcGIS Engine 项目中添加 MuneStirp 控件,该控件用于显示测量结果。
2. 在菜单栏中添加“测量距离”按钮,单击该按钮后,进入测量模式。
3. 在地图上单击两个点,计算它们之间的距离,并将结果显示在 MuneStirp 控件中。
下面是具体实现方法:
1. 添加 MuneStirp 控件
在 Visual Studio 中打开 ArcGIS Engine 项目,右键单击工具箱中的“常规”选项卡,选择“选择项”,在“COM 组件”选项卡中选中“MapControl Tools 1.0 Type Library”和“MapControl 1.0 Type Library”,单击“确定”按钮,将 MuneStirp 控件添加到工具箱中。
在窗体设计器中,将 MuneStirp 控件拖放到窗体上,调整其大小和位置。
2. 添加测量按钮
在窗体设计器中,右键单击菜单栏,选择“添加新项”,创建一个名为“测量距离”的按钮。
在按钮的 Click 事件中,添加以下代码:
```csharp
private void btnMeasureDistance_Click(object sender, EventArgs e)
{
// 进入测量模式
axMapControl1.CurrentTool = null;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
axMapControl1.MouseDownEvent += new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent);
}
```
3. 实现测量距离功能
在 axMapControl1_MouseDownEvent 事件中,获取鼠标单击的坐标,并计算两点之间的距离。将距离显示在 MuneStirp 控件中。
```csharp
private void axMapControl1_MouseDownEvent(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1) // 左键单击
{
if (axMapControl1.CurrentTool == null) // 如果不在测量模式中
{
// 进入测量模式
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
double x1 = e.mapX;
double y1 = e.mapY;
IPoint point1 = new PointClass();
point1.PutCoords(x1, y1);
IGraphicsContainer graphicsContainer = axMapControl1.ActiveView as IGraphicsContainer;
IElement element1 = GetPointElement(point1);
graphicsContainer.AddElement(element1, 0);
axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
axMapControl1.MouseDownEvent -= new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent);
axMapControl1.MouseDownEvent += new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent2);
}
}
}
private void axMapControl1_MouseDownEvent2(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1) // 左键单击
{
double x2 = e.mapX;
double y2 = e.mapY;
IPoint point2 = new PointClass();
point2.PutCoords(x2, y2);
IGraphicsContainer graphicsContainer = axMapControl1.ActiveView as IGraphicsContainer;
IElement element2 = GetPointElement(point2);
graphicsContainer.AddElement(element2, 0);
axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
// 计算距离
double distance = GetDistance(point1, point2);
IFormattedTextSymbol symbol = new TextSymbolClass();
symbol.Background = GetColor(255, 255, 255);
symbol.Border = true;
symbol.Color = GetColor(0, 0, 0);
symbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;
symbol.VerticalAlignment = esriTextVerticalAlignment.esriTVABottom;
symbol.Angle = GetAngle(point1, point2);
ITextElement textElement = new TextElementClass();
textElement.Symbol = symbol;
textElement.Text = string.Format("{0:0.00} 米", distance);
textElement.ScaleText = true;
IElement element3 = textElement as IElement;
element3.Geometry = GetMidPoint(point1, point2);
graphicsContainer.AddElement(element3, 0);
axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
// 显示结果
muneStrip.Items.Clear();
muneStrip.Items.Add(string.Format("距离:{0:0.00} 米", distance));
muneStrip.Visible = true;
// 退出测量模式
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
axMapControl1.MouseDownEvent -= new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent2);
axMapControl1.MouseDownEvent += new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_MouseDownEvent);
}
}
private IElement GetPointElement(IPoint point)
{
ISimpleMarkerSymbol symbol = new SimpleMarkerSymbolClass();
symbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
symbol.Color = GetColor(255, 0, 0);
symbol.Size = 6;
ISimpleMarkerSymbol symbol2 = new SimpleMarkerSymbolClass();
symbol2.Style = esriSimpleMarkerStyle.esriSMSCircle;
symbol2.Color = GetColor(255, 255, 255);
symbol2.Size = 4;
IMultiLayerMarkerSymbol multiLayerMarkerSymbol = new MultiLayerMarkerSymbolClass();
multiLayerMarkerSymbol.AddLayer(symbol);
multiLayerMarkerSymbol.AddLayer(symbol2);
IMarkerElement markerElement = new MarkerElementClass();
markerElement.Symbol = multiLayerMarkerSymbol as IMarkerSymbol;
IElement element = markerElement as IElement;
element.Geometry = point;
return element;
}
private double GetDistance(IPoint point1, IPoint point2)
{
IProximityOperator proximityOperator = point1 as IProximityOperator;
return proximityOperator.ReturnDistance(point2);
}
private double GetAngle(IPoint point1, IPoint point2)
{
double dx = point2.X - point1.X;
double dy = point2.Y - point1.Y;
double angle = Math.Atan2(dy, dx) * 180 / Math.PI;
return angle;
}
private IPoint GetMidPoint(IPoint point1, IPoint point2)
{
IPoint midPoint = new PointClass();
midPoint.X = (point1.X + point2.X) / 2;
midPoint.Y = (point1.Y + point2.Y) / 2;
return midPoint;
}
private IColor GetColor(int red, int green, int blue)
{
IRgbColor color = new RgbColorClass();
color.Red = red;
color.Green = green;
color.Blue = blue;
return color;
}
```
以上代码中,GetPointElement 方法用于创建点元素;GetDistance 方法用于计算两点之间的距离;GetAngle 方法用于计算两点之间的夹角;GetMidPoint 方法用于计算两点之间的中点;GetColor 方法用于创建颜色对象。
阅读全文