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

时间:2023-03-10 03:29:09
基于ArcEngine与C#的鹰眼地图实现

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

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

 private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
if (axMapControl1.Map.LayerCount > )
{
for (int i = ; i <= axMapControl1.Map.LayerCount - ; i++)
{
axMapControl2.AddLayer(axMapControl1.get_Layer(i));
}
axMapControl2.Extent=axMapControl1.Extent;
axMapControl2.Refresh();
}
}

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

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

         private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
{
//获得一个新范围
IEnvelope pEnvelope = (IEnvelope)e.newEnvelope;
IGraphicsContainer pGrahicsContainer = axMapControl2.Map as IGraphicsContainer;
IActiveView pActiveView = pGrahicsContainer as IActiveView;
//在绘制前清除axMapControl2中所有图层
pGrahicsContainer.DeleteAllElements();
IRectangleElement pRectangleElement = new RectangleElementClass();
IElement pElement = pRectangleElement as IElement;
pElement.Geometry = pEnvelope;
//设置鹰眼图中的红线框
IRgbColor pColor = new RgbColorClass();
pColor.Red = ;
pColor.Green = ;
pColor.Blue = ;
pColor.Transparency = ;
//产生一个线符号对象
ILineSymbol pOutline = new SimpleLineSymbolClass();
pOutline.Width = ;
pOutline.Color = pColor;
//设置填充符号颜色
pColor = new RgbColorClass();
pColor.Red = ;
pColor.Green = ;
pColor.Blue = ;
pColor.Transparency = ;
//设置填充符号
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pColor;
pFillSymbol.Outline = pOutline; IFillShapeElement pFillShapeEle = pElement as IFillShapeElement;
pFillShapeEle.Symbol = pFillSymbol;
pGrahicsContainer.AddElement((IElement)pFillShapeEle, );
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }

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

  private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if (axMapControl2.Map.LayerCount > )
{
if (e.button == )
{
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
axMapControl1.CenterAt(pPoint);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
else if (e.button == )
{
IEnvelope pEnv = axMapControl2.TrackRectangle();
axMapControl1.Extent = pEnv;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}
}

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

  private void axMapControl2_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.button == )
{
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
axMapControl1.CenterAt(pPoint);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}

三、结论

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

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

《ArcGIS Engine 10 开发手册》

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

《ArcObjects Developer Help》