1、距离测量

距离测量时,片段长度通过两点之间距离计算得到,全部长度通过片段长度的和计算得到。主要用到INewLineFeedback和IScreenDisplay两个接口。

1)INewLineFeedback接口

该接口的主要方法如表:

方法描述
Start从指定点开始绘制追踪线的绘制
MoveTo鼠标移动到的点,并实时绘制与上一节点的连接线
AddPoint添加一个点
Stop停止追踪线的绘制,并返回用户绘制的几何体

2)IScreenDisplay接口

MapControl中的每一个视图都有一个ScreenDisplay对象,用于控制视图中的图形绘制。ScreenDisplay对象除了管理窗体屏幕的显示属性外,也管理缓存和视图屏幕的变化等。可以通过IActiveView接口的ScreenDisplay属性获取ScreenDisplay对象。通ScreenDisplay对象的DisplayTransformation属性进行设备单位和地图单位的转换。

3)实现的思路

    • 鼠标点击时,首先判断INewLineFeedback接口的实例化对象pNewLineFeedback是否为空,如果为空就实例化,并设当前的鼠标点为pNewLineFeedback的起始点。反之,则把当前鼠标点添加到pNewLineFeedback中。
    • 鼠标移动时。实时计算鼠标移动点与上一鼠标点击点的距离,以及所画线的长度。
    • 鼠标双击时,停止绘制,并清空pNewLineFeedback对象
    • btnMeasureLine的单击事件打结果窗体,并设置bMeasurelength为true,以便从数据视图的鼠标事件中判断量算功能是否开启。

2、面积量测

面积量测通过INewPolygonFeedback接口绘制多边形来实现。使用NewPolygonFeedback和使用NewLineFeedback的方法类似,但其显示和返回的几何特征是一个封闭多边形,这意味着画多边形停止时,起点将成为终点,从而结束形状的绘制,该方法至少需要三个点被添加到几何对象中。

实现的思路如下

1)、点击鼠标时首先判断INewPolygonFeedback接口的实例化对象pNewPolygonFeedback是否为空,则实例化,并设当前鼠标点为pNewPolygonFeedback的起始点;反之则把当前的鼠标点添加到pNewPolygonFeedback中。

2)、鼠标移动时,判断绘制多边形时点的个数pPointCol是否超过3个点,如果超过三个,则由点击构建IPolygon接口,IArea接口,进而计算出面的总长度和面积。

3)、鼠标双击时,停止绘制,并清空pNewPloygonFeedback对象。


3、面积测量和距离测量的具体代码分布:

1.前提:新建一个用来显示测量结果的公共窗体
2.二者的button_click事件:
  #region 距离的测量按钮单击事件
private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
{
mainMapControl.CurrentTool = null;
pMouseOperate = "MeasureLength";
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
if (frmMeasureResult == null || frmMeasureResult.IsDisposed)
{
frmMeasureResult = new FormMeasureResult();
frmMeasureResult.frmClosed += new FormMeasureResult.FormClosedEventHandler(frmMeasureResult_frmColsed);
frmMeasureResult.labToltal.Text = "";
frmMeasureResult.Text = "距离量测";
frmMeasureResult.Show();
}
else
{
frmMeasureResult.Activate();
}
}
#endregion
  #region 面积测量按钮单击事件
private void btnPolyMeasure_ItemClick(object sender, ItemClickEventArgs e)
{
mainMapControl.CurrentTool = null;
pMouseOperate = "MeasureArea";
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;//光标样式为十字丝
if (frmMeasureResult == null || frmMeasureResult.IsDisposed)
{
frmMeasureResult = new FormMeasureResult();
frmMeasureResult.frmClosed += new FormMeasureResult.FormClosedEventHandler(frmMeasureResult_frmColsed); ;
frmMeasureResult.labToltal.Text = "";
frmMeasureResult.Text = "面积测量";
frmMeasureResult.Show();
}
else
{
frmMeasureResult.Activate();
}
}
#endregion
3.在mainForm中定义全局变量
  #region 变量的定义
//变量的定义
//距离和面积量测的变量
private FormMeasureResult frmMeasureResult = null;//量算的结果窗体
private INewLineFeedback pNewLineFeedback;//追踪线对象
private INewPolygonFeedback pNewPolyFeedback;//追踪面对象
private IPoint pPoint = null;//鼠标点击点
private IPoint pMovePt = null;//鼠标移动时的当前点
private double dToltalLength = ;//量测总长度
private double dSegmentLength = ;//片段距离
private IPointCollection pArearPointCol = new MultipointClass();//对面积量算时画的点进行存储
//定义状态栏坐标显示工具
private string sMapUnits = "未知单位";//地图单位变量
private object missing = Type.Missing;
#endregion
4.在mianMapControl控件的OnMouse_Down事件中进行switch语句注册
 #region   距离量测
case "MeasureLength":
//判断追踪线对象是否为空,若是则实例化并设置当前鼠标点为起始点
if (pNewLineFeedback == null)
{
//实例化追踪线对象
pNewLineFeedback = new NewLineFeedbackClass();
pNewLineFeedback.Display = (mainMapControl.Map as IActiveView).ScreenDisplay;
//设置起点,开始动态线绘制
pNewLineFeedback.Start(pPoint);
dToltalLength = ;
}
else //如果追踪线对象不为空,则添加当前鼠标点
{
pNewLineFeedback.AddPoint(pPoint);
}
//pGeometry = m_PointPt;
if (dSegmentLength != )
{
dToltalLength = dToltalLength + dSegmentLength;
}
break;
#endregion
#region 面积测量
case "MeasureArea":
if (pNewPolyFeedback == null)
{
//实例化追踪面对象
pNewPolyFeedback = new NewPolygonFeedback();
pNewPolyFeedback.Display = (mainMapControl.Map as IActiveView).ScreenDisplay;
pArearPointCol.RemovePoints(, pArearPointCol.PointCount);
//开始绘制多边形
pNewPolyFeedback.Start(pPoint);
pArearPointCol.AddPoint(pPoint, ref missing, ref missing);
}
else
{
pNewPolyFeedback.AddPoint(pPoint);
pArearPointCol.AddPoint(pPoint,ref missing,ref missing ); }
break;
#endregion
5.在mapControl的OnMouseMove事件中进行设置鼠标移动的实时测量结果
  #region 长度量算 实时显示测量结果
if (pMouseOperate == "MeasureLength")
{
if (pNewLineFeedback != null)
{
pNewLineFeedback.MoveTo(pMovePt);
}
double deltaX = ; //两点之间X差值
double deltaY = ; //两点之间Y差值 if ((pPoint != null) && (pNewLineFeedback != null))
{
deltaX = pMovePt.X - pPoint.X;
deltaY = pMovePt.Y - pPoint.Y;
dSegmentLength = Math.Round(Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY)), );
dToltalLength = dToltalLength + dSegmentLength;
if (frmMeasureResult != null)
{
frmMeasureResult.labToltal .Text = String.Format("当前线段长度{0}{1}",dSegmentLength,sMapUnits );
frmMeasureResult.labToltalLength.Text = String.Format("总长度:{0}{1}",dToltalLength,sMapUnits );
//// frmMeasureResult.labToltal.Text = String.Format(
// "当前线段长度:{0:.###}{1};\r\n总长度为: {2:.###}{1}",
// dSegmentLength, sMapUnits, dToltalLength);
dToltalLength = dToltalLength - dSegmentLength; //鼠标移动到新点重新开始计算
}
frmMeasureResult.frmClosed += new FormMeasureResult.FormClosedEventHandler(frmMeasureResult_frmColsed);
}
}
#endregion
#region 面积量算
if(pMouseOperate=="MeasureArea")
{
if(pNewPolyFeedback!=null)
{
pNewPolyFeedback .MoveTo(pMovePt);
}
IPointCollection pPointCol=new Polygon();
IPolygon pPolygon=new PolygonClass();
IGeometry pGeo=null;
ITopologicalOperator pTopo=null;
for(int i=;i<=pArearPointCol.PointCount-;i++)
{
pPointCol.AddPoint(pArearPointCol.get_Point(i),ref missing,ref missing);
}
pPointCol.AddPoint(pMovePt,ref missing,ref missing);
if(pPointCol.PointCount<) return;
pPolygon=pPointCol as IPolygon;
if((pPolygon!=null))
{
pPolygon.Close();
pGeo=pPolygon as IGeometry;
pTopo=pGeo as ITopologicalOperator ;
//使几何图形的拓扑正确
pTopo.Simplify();
pGeo.Project(mainMapControl.Map.SpatialReference);
IArea pArea=pGeo as IArea ;//面积量算所画的面
frmMeasureResult.labToltal.Text=String.Format("总面积为:{0}平方{1}",pArea.Area,sMapUnits);
frmMeasureResult.labToltalLength.Text=String.Format("总长度为:{0}{1}",pPolygon.Length,sMapUnits);
pPolygon=null;
}
}
#endregion
6.mainMapControl的OnDoubleclick事件中
  #region 距离量测的结束设置
if (pMouseOperate == "MeasureLength")
{
if (frmMeasureResult != null)
{
frmMeasureResult .labToltalLength .Text = "线段总长度为:" + dToltalLength + sMapUnits;
}
if (pNewLineFeedback != null)
{
pNewLineFeedback.Stop();
pNewLineFeedback = null;
//清空所画的线对象
(mainMapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null);
}
dToltalLength = ;
dSegmentLength = ;
}
#endregion
#region 面积测量的结束设置
if (pMouseOperate == "MeasureArea")
{
if (pNewPolyFeedback != null)
{
pNewPolyFeedback.Stop();
pNewPolyFeedback = null;
//清空所画的线对象
(mainMapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null);
}
pArearPointCol.RemovePoints(, pArearPointCol.PointCount);//清空点集中的所有点
}
#endregion

综上述:一个完美的距离和面积测量的工具就完成了!欢迎同行们相互交流和学习。

05-07 09:56