//主地图的地图(map)对象 IMap map = null; IActiveView activeView = null; //IGraphicsContainer用于操做临时元素,能够经过map获取 IGraphicsContainer gc = map as IGraphicsContainer; //删除全部临时元素 gc.DeleteAllElements(); activeView.Refresh(); //画点的临时元素~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IPoint point = new PointClass(); point.PutCoords(100, 200); //首先定义点元素的样式 //ISimpleMarkerSymbol意思是ISimple(简单的)Marker(点)Symbol(样式),MarkerSymbol处理simple的还有其余不少种,具体看IMarkerSymbol的实现类 ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass(); //点颜色 simpleMarkerSymbol.Color = SymbolHelper.CreateColorByRgb(255, 0, 0); //点大小 simpleMarkerSymbol.Size = 5; //IMarkerElement表明点元素, new MarkerElementClass()是实例化点元素 IMarkerElement markerElement = new MarkerElementClass(); //设置点样式 markerElement.Symbol = simpleMarkerSymbol; //IElement是全部元素(element)的顶层接口 IElement element = markerElement as IElement; //设置元素几何对象,由于是画点因此赋值一个点 //经过观察以后的添加线和面元素可发现,几何对象赋值都在IElement接口,而样式(symbol)赋值都在各类类型元素的接口 element.Geometry = point; //添加元素到地图,最后刷新,完成添加 gc.AddElement(element, 0); activeView.Refresh(); //画线的临时元素~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //线的生成不是重点,这里就随便了 IPolyline polyline = null; //定义线样式 //ISimpleLineSymbol意思是ISimple(简单的)Line(线)Symbol(样式) ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass(); //颜色 simpleLineSymbol.Color = SymbolHelper.CreateColorByRgb(255, 0, 0); //线宽 simpleLineSymbol.Width = 2; //ILineElement表明线元素, new LineElementClass()是实例化线元素 ILineElement lineElement = new LineElementClass(); //赋值线样式 lineElement.Symbol = simpleLineSymbol; //IElement是全部元素(element)的顶层接口 element = lineElement as IElement; //设置元素几何对象,由于是画线因此赋值一个线 element.Geometry = polyline; //添加元素到地图,最后刷新,完成添加 gc.AddElement(element, 0); activeView.Refresh(); //画面暂时略 //以上是画临时元素的详细代码解析,在实际使用中,通常能够使用封装好的方法一行代码解决 //画点 DrawElementHelper.DrawPoint(map, point, 255, 0, 0, 3); //画线 DrawElementHelper.DrawLine(map, polyline, 255, 0, 0, 3); //以上方法没有刷新,需另外调用刷新 //PS:所以若是同时画多个元素,每次画都刷新会很卡 activeView.Refresh();
上述代码调用的封装接口code
/// <summary> /// 经过rgb建立颜色 /// </summary> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public static IColor CreateColorByRgb(int r, int g, int b) { IRgbColor color = new RgbColor(); color.Red = r; color.Green = g; color.Blue = b; return color as IColor; } /// <summary> /// 画点 /// 不带刷新 /// </summary> /// <param name="map"></param> /// <param name="point"></param> /// <param name="r">颜色r</param> /// <param name="g">颜色g</param> /// <param name="b">颜色b</param> /// <param name="size">点大小</param> /// <returns></returns> public static IMarkerElement DrawPoint(IMap map, IPoint point, int r, int g, int b, double size) { ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass(); //点颜色 simpleMarkerSymbol.Color = SymbolHelper.CreateColorByRgb(r, g, b); //点大小 simpleMarkerSymbol.Size = size; //IMarkerElement表明点元素, new MarkerElementClass()是实例化点元素 IMarkerElement markerElement = new MarkerElementClass(); //设置点样式 markerElement.Symbol = simpleMarkerSymbol; //IElement是全部元素(element)的顶层接口 IElement element = markerElement as IElement; //设置元素几何对象,由于是画点因此赋值一个点 //经过观察以后的添加线和面元素可发现,几何对象赋值都在IElement接口,而样式(symbol)赋值都在各类类型元素的接口 element.Geometry = point; IGraphicsContainer gc = map as IGraphicsContainer; gc.AddElement(element, 0); return markerElement; } /// <summary> /// 画线 /// 不带刷新 /// </summary> /// <param name="map"></param> /// <param name="polyline"></param> /// <param name="r">颜色r</param> /// <param name="g">颜色g</param> /// <param name="b">颜色b</param> /// <param name="width">线宽</param> /// <returns></returns> public static ILineElement DrawLine(IMap map, IPolyline polyline, int r, int g, int b, double width) { ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass(); //颜色 simpleLineSymbol.Color = SymbolHelper.CreateColorByRgb(r, g, b); //线宽 simpleLineSymbol.Width = width; //ILineElement表明线元素, new LineElementClass()是实例化线元素 ILineElement lineElement = new LineElementClass(); //赋值线样式 lineElement.Symbol = simpleLineSymbol; //IElement是全部元素(element)的顶层接口 IElement element = lineElement as IElement; //设置元素几何对象,由于是画线因此赋值一个线 element.Geometry = polyline; IGraphicsContainer gc = map as IGraphicsContainer; gc.AddElement(element, 0); return lineElement; }