在使用ArcGIS的过程中,我们会发现在右上角有个长度量算的工具,对于随意在地图上选择的两点进行量算并给出实际距离。那么到底是怎么实现的呢,本文附上实现的源代码,有错误或者更好地优化的方案请进行纠正,努力才会让目标离我们更近些。
//长度量算工具代码
private IHookHelper m_hookHelper;
private IGraphicsContainer pGraphicContainer;
private IMap pMap;
private IActiveView pActiveView;
private IDisplayFeedback pDisplayFeedback;
private IScreenDisplay pScreenDisplay = null;
IPoint pPointStd;
IPoint pPoint;
IPoint pPointLast;
IProximityOperator pProximity;
public double measureDistance = 0;
public double DistanceSegment = 0;
INewLineFeedback pNewLineFeedback = null;
public DistancePointsTool()
{
base.m_category="SpatailAnalyst";
base.m_caption="长度量算";
base.m_message="长度量算";
base.m_toolTip="长度量算";
base.m_name="长度量算";
}
public override void OnCreate(object hook)
{
if (m_hookHelper==null)
m_hookHelper=new HookHelperClass();
m_hookHelper.Hook=hook;
pActiveView=m_hookHelper.ActiveView;
pMap=pActiveView.FocusMap;
pGraphicContainer=pMap as IGraphicsContainer;
pDisplayFeedback=null;
pScreenDisplay=pActiveView.ScreenDisplay;
}
//重载鼠标点击事件
protected override void OnMouseDown(int Button,int shift,int X,int Y)
{
//产生一个当前点击的点对象
if (Button==1)
{
//判断该点是否为第一个点
if (pDisplayFeedback==null)
{
pPointStd=new PointClass();
pPointStd.SpatialReference=pMap.SpatialReference;
pPointStd=pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y);
pDisplayFeedback=new NewLineFeedbackClass();
pDisplayFeedback.Display=pScreenDisplay;
pNewLineFeedback=pDisplayFeedback as INewLineFeedback;
pNewLineFeedback.Start(pPointStd);
pPointLast=pPointStd;
pProximity=pPointStd as IProximityOperator;
}
else{
pNewLineFeedback=pDisplayFeedback as INewLineFeedback;
pNewLineFeedback.AddPoint(pPoint);
IProximityOperator pProximity=pPointLast as IProximityOperator;
DistanceSegment=pProximity.ReturnDistance(pPoint);
measureDistance+=pProximity.ReturnDistance(pPoint);
pPointLast =pPoint;
}
}
}
//重载鼠标移动事件
public override void OnMouseMove(int Button,int shift,int X,int Y)
{
pPoint =new PointClass();
pPoint.SpatialReference=pMap.SpatialReference;
pPoint=pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y);
if(pNewLineFeedback!=null)
{
pNewLineFeedback.MoveTo(pPoint);
}
}
//重载鼠标双击事件,距离量测结束
public override void onDblClick()
{
base.onDblClick();
IGeometry pGeometry=null;
string LengthUnits=null;
if (pNewLineFeedback!=null)
{
pGeometry=pNewLineFeedback.Stop();
pNewLineFeedback=null;
IElement pElement=null;
ISimpleLineSymbol pSimpleLineSymbol=new SimpleLineSymbolClass();
pSimpleLineSymbol.Width=2;
pSimpleLineSymbol.Style=esriSimpleLineStyle.esriSLSSolid;
ISimpleFillSymbol pSimpleFillSymbol=new SimpleFillSymbolClass();
pSimpleFillSymbol.Style=esriSimpleFillStyle.esriSFSSolid;
pSimpleFillSymbol.Outline= pSimpleLineSymbol;
ILineElement pLineElement=new LineElementClass();
pLineElement.Symbol=pSimpleLineSymbol;
pElement=pLineElement as IElement;
pElement=pLineElement as IElement;
pElement.Geometry=pGeometry;
IGraphicsContainer pGraphicsContainer=pActiveView.FocusMap as IGraphicsContainer;
pGraphicsContainer.AddElement(pElement,0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);
pGraphicsContainer.DeleteElement(pElement);
MessageBox.Show("\n线段长度:"+DistanceSegment.ToString()+"\n总长度:"+measureDistance.ToString()+"\n地图单位:"+LengthUnits,"长度量算");
measureDistance=0;
DistanceSegment=0;
pNewLineFeedback=null;
pDisplayFeedback=null;
}
}