画线,首先得到线对象,要用到 IPointCollection 和 IPolyline 对象,
然后加载线元素IElement。
/// <summary>
/// 画线
/// <param name="arrPointAll">点坐标数组</param>
///<returns></returns>
private void DrawLine(ArrayList arrPointAll)
{
if (arrPointAll.Count <= 0)//点坐标数组不能为空
{
return;
}
if (activeView == null)
{
activeView = this.MapControl.ActiveView.FocusMap as IActiveView;
}
//删除以前的element
DeleteOldElement(activeView.GraphicsContainer);
// 获取IRGBColor接口
IRgbColor color = new RgbColor();
// 设置颜色属性
color.Red = 255;
color.Transparency = 255;
//点
IPoint pPoint = new PointClass();
//线样式
ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
lineSymbol.Color = color;
lineSymbol.Style = esriSimpleLineStyle.esriSLSInsideFrame;
lineSymbol.Width = 1;
//线元素
ILineElement lineElement = new LineElementClass();
lineElement.Symbol = lineSymbol;
//创建线
IPolyline m_Polyline = new PolylineClass();
//点集合
IPointCollection m_PointCollection = new PolylineClass();
//点数组
ArrayList arrPoint = new ArrayList();
object missing = Type.Missing;
foreach (object o in arrPointAll)
{
arrPoint = (ArrayList)o;
pPoint.PutCoords(int.Parse(arrPoint[0].ToString()), int.Parse(arrPoint[1].ToString()));
m_PointCollection.AddPoint(pPoint, ref missing, ref missing);
}
//QI for IPolyline
m_Polyline = m_PointCollection as IPolyline;
//放大地图
//折线范围
IEnvelope pEnvelope = m_Polyline.Envelope;
//折线区域
IArea pArea = pEnvelope as IArea;
pPoint = pArea.Centroid;
this.ChangeEnvelope(pPoint, 0.06, 0.06);
//QI for IElement
IElement element = lineElement as IElement;
element.Geometry = m_Polyline;
//加载线元素到地图
activeView.GraphicsContainer.AddElement(element, 0);
//Refresh the graphics
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}