Arcgis Engine(ae)接口详解(8):临时元素(element)
分享
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();
上述代码调用的封装接口 /// <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;
}
0 个评论
相关问题
- 如何优雅的使用ArcGIS知乎?
- 建设ArcGIS知乎平台的目和意义?
- ArcGIS知乎社区改进意见征集
- 【抽奖结果已出】ArcGIS知乎社区活动 之 你不知道的地理空间革命【转发分享有奖】【附奖品寄送照片】
- 苹果系统下可以使用arcgis 吗?
- 请问ArcGIS适合在局域网内用javaweb做二次开发吗?
- arcgis版本兼容性
- ArcGIS 知乎社区活动 之 字幕组加鸡腿暨KBP字幕组招新【获奖名单公布与优秀作品展示】
- 请问大神们,怎么用ArcGIS做城镇间经济联系强度的图?
- 在Visual studio 2015 中使用 Arcgis API for JavaScript v3.16 如何实现代码智能提示?
- ArcGIS如何求栅格图像的“质心”