基于ArcEngine与C#的鹰眼地图实现

鹰眼图是对全局地图的一种概略表达,具有与全局地图的空间参考和空间范围。为了更好起到空间提示和导航作用,有些还具备全局地图中重要地理要素,如主要河流、道路等的概略表达。通过两个axMapControl控件,主控件axMapControl 1和鹰眼控件axMapControl 2。要实现鹰眼功能,关键技术有两点,一是如何让两个控件使用的数据保持一致,另一点是如何绘制鹰眼控件中的显示方框。

一、数据共享,使用axMapControl1的控件的OnMapReplaced事件。OnMapReplace事件发生MapControl的地图被替换后,即IMapControl2::Map被另一个地图替换时(如IMapControl2::LoadMxFile方法被调用时或map属性被明确替换时)触发该事件。用这个事件来保持与当前图层同步。

 1 private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
 2         {
 3             if (axMapControl1.Map.LayerCount > 0)
 4             {
 5                 for (int i = 0; i <= axMapControl1.Map.LayerCount - 1; i++)
 6                 {
 7                     axMapControl2.AddLayer(axMapControl1.get_Layer(i));
 8                 }
 9                 axMapControl2.Extent=axMapControl1.Extent;
10                 axMapControl2.Refresh();
11             }
12         }
 

二、显示方框的绘制。在鹰眼控件axMapControl2中使用鼠标拖曳视图时,鹰眼控件axMapControl2中出现红色矩形框。

1)axMapControl1控件的OnExtentUpdated事件。OnExtentUpdated事件在MapControl的可视化范围发生变化后发生,即当IMapControl2::Extent属性发生变化时被触发。改变MapControl中可视化范围的途径包括精确设置范围、缩放、漫游或使用IMapControl2::CenterAt方法等。

 1         private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
 2         {
 3             //获得一个新范围
 4             IEnvelope pEnvelope = (IEnvelope)e.newEnvelope;
 5             IGraphicsContainer pGrahicsContainer = axMapControl2.Map as IGraphicsContainer;
 6             IActiveView pActiveView = pGrahicsContainer as IActiveView;
 7             //在绘制前清除axMapControl2中所有图层
 8             pGrahicsContainer.DeleteAllElements();
 9             IRectangleElement pRectangleElement = new RectangleElementClass();
10             IElement pElement = pRectangleElement as IElement;
11             pElement.Geometry = pEnvelope;
12             //设置鹰眼图中的红线框
13             IRgbColor pColor = new RgbColorClass();
14             pColor.Red = 255;
15             pColor.Green = 0;
16             pColor.Blue = 0;
17             pColor.Transparency = 255;
18             //产生一个线符号对象
19             ILineSymbol pOutline = new SimpleLineSymbolClass();
20             pOutline.Width = 3;
21             pOutline.Color = pColor;
22             //设置填充符号颜色
23             pColor = new RgbColorClass();
24             pColor.Red = 255;
25             pColor.Green = 0;
26             pColor.Blue = 0;
27             pColor.Transparency = 0;
28             //设置填充符号
29             IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
30             pFillSymbol.Color = pColor;
31             pFillSymbol.Outline = pOutline;
32 
33             IFillShapeElement pFillShapeEle = pElement as IFillShapeElement;
34             pFillShapeEle.Symbol = pFillSymbol;
35             pGrahicsContainer.AddElement((IElement)pFillShapeEle, 0);
36             pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
37 
38         }

2)axMapControl2控件的OnMouseDown事件。当在MapControl上点击鼠标任何键时触发OnMouseDown事件。

 1  private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
 2         {
 3             if (axMapControl2.Map.LayerCount > 0)
 4             {
 5                 if (e.button == 1)
 6                 {
 7                     IPoint pPoint = new PointClass();
 8                     pPoint.PutCoords(e.mapX, e.mapY);
 9                     axMapControl1.CenterAt(pPoint);
10                     axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
11                 }
12                 else if (e.button == 2)
13                 {
14                     IEnvelope pEnv = axMapControl2.TrackRectangle();
15                     axMapControl1.Extent = pEnv;
16                     axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
17                 }
18             }
19         }

3)axMapControl2控件的OnMouseMove事件。当在MapControl上移动鼠标时不断地触发OnMouseMove事件。

 1  private void axMapControl2_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
 2         {
 3             if (e.button == 1)
 4             {
 5                 IPoint pPoint = new PointClass();
 6                 pPoint.PutCoords(e.mapX, e.mapY);
 7                 axMapControl1.CenterAt(pPoint);
 8                 axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
 9             }
10         }

三、结论

当axMapControl1中的数据被替换时,axMapControl2中的数据会自动加载axMapControl1的所有图层,实现两者的数据统一。在axMapControl2控件中拖曳或移动地图时,axMapControl1中的地图也在随时变化。

参考文献:基于ArcGIS Engine与C#.net的地图鹰眼功能的实现 南峰

              《ArcGIS Engine 10 开发手册》

              《ArcObjects二次开发教程》傅仲良 主编

              《ArcObjects Developer Help》

           

宣言:在此记录自己学习过程中的心得体会,同时积累经验,不断提高自己! 文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家留言交流。转载请注明出处。
原文地址:https://www.cnblogs.com/vegetable/p/4101844.html