距离测量
实现在IMapcontrol上采点,双击结束采点进行距离测量,并标注在IMapcontrol上,实现结果如下:
1. 初始化主要变量:
1 private bool m_bStart = false; 2 private INewLineFeedback m_pNewLineFeedback = null; 3 private IPoint m_prevPoint = null; 4 private double m_CurDistance = 0.0; 5 private double m_TotalDistance = 0.0;
2.MouseDown事件:
1 IPoint pt = m_pMapControl.ToMapPoint(X, Y); 2 3 if (m_bStart && m_pNewLineFeedback != null) 4 { 5 m_pNewLineFeedback.AddPoint(pt); 6 if (m_prevPoint != null) 7 { 8 m_CurDistance = ReturnDistance(m_prevPoint, pt); 9 m_TotalDistance += m_CurDistance; 10 m_prevPoint = pt; 11 } 12 } 13 else 14 { 15 m_pNewLineFeedback.Display = m_pMapControl.ActiveView.ScreenDisplay; 16 m_bStart = true; 17 m_pNewLineFeedback.Start(pt); 18 m_prevPoint = pt; 19 }
3.MouseDoubleClick事件:
1 try 2 { 3 IPolyline pline = m_pNewLineFeedback.Stop(); 4 IGraphicsContainer pGraphicsContainer = m_pMapControl.ActiveView as IGraphicsContainer; 5 6 DrawElement(pline, pGraphicsContainer); 7 } 8 catch (Exception ex) 9 { 10 System.Windows.Forms.MessageBox.Show(ex.ToString(), "提示"); 11 12 } 13 m_TotalDistance = 0; 14 m_bStart = false;
4.在Mapcontrol上添加标注:
添加所画线的标注:
1 IRgbColor pColor = new RgbColorClass(); 2 ILineElement pLineElement = new LineElementClass(); 3 IElement pElement = pLineElement as IElement; 4 pElement.Geometry = pLine as IGeometry; 5 6 ISimpleLineSymbol pSmplLine = new SimpleLineSymbolClass(); 7 pColor = new RgbColorClass(); 8 pColor.Red = 0; pColor.Blue = 250; pColor.Green = 0; 9 pSmplLine.Color = pColor as IColor; 10 pSmplLine.Style = esriSimpleLineStyle.esriSLSSolid; 11 pSmplLine.Width = 1; 12 pLineElement.Symbol = pSmplLine as ILineSymbol; 13 14 pGraphicsContainer.AddElement(pElement, 0);
添加文字标注:
1 ITextElement pTextElement = new TextElementClass(); 2 pElement = pTextElement as IElement; 3 pElement.Geometry = pLine as IGeometry; 4 ISimpleTextSymbol pSimpleTextSymbol = new TextSymbolClass(); 5 6 pColor.Red = 255; pColor.Green = 0; pColor.Blue = 0; 7 pSimpleTextSymbol.Color = pColor as IColor; 8 stdole.IFont font = new stdole.StdFontClass(); 9 font.Name = "宋体"; 10 font.Bold = false; 11 font.Size = 18; 12 pSimpleTextSymbol.Font = font as IFontDisp; 13 14 pTextElement.Symbol = (ITextSymbol)pSimpleTextSymbol; 15 pTextElement.ScaleText = false; 16 pTextElement.Text = "距离:" + pLine.Length.ToString("0.00") + " 米"; 17 pGraphicsContainer.AddElement(pElement, 0);
刷新显示:
1 m_pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
另:返回两个IGeometry之间的距离:
1 private double ReturnDistance(IPoint point1, IPoint point2) 2 { 3 IProximityOperator pPO = point1 as IProximityOperator; 4 double dis = pPO.ReturnDistance(point2); 5 return dis; 6 }