所谓的鹰眼,就是一个缩略地图,上面有一个矩形框,矩形框区域就是当前显示的地图区域,拖动矩形框可以改变当前地图显示的位置,改变矩形框的大小,可以改变当前地图的显示区域大小,从起到导航的作用。鹰眼是地图浏览中常用的功能之一。
如下图:
具体实现:
一、载入地图到鹰眼控件:
//当主地图显示控件的地图更换时,鹰眼中的地图也跟随更换 private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e) { this.axMapControl2.Map = new Map(); // 添加主地图控件中的所有图层到鹰眼控件中 for (int i = 1; i <= this.axMapControl1.LayerCount; i++) { this.axMapControl2.AddLayer(this.axMapControl1.get_Layer(this.axMapControl1.LayerCount - i)); } // 设置 MapControl 显示范围至数据的全局范围 this.axMapControl2.Extent = this.axMapControl1.FullExtent; // 刷新鹰眼控件地图 this.axMapControl2.Refresh(); }
二、绘制鹰眼矩形框:
为鹰眼控件 MapControl1 添加 OnExtentUpdated 事件,此事件是在主 Map 控件的显示范围改变时响应,从而相应更新鹰眼控件中的矩形框。其响应函数代码如下:
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e) { // 得到新范围 IEnvelope pEnv = (IEnvelope)e.newEnvelope; IGraphicsContainer pGra = axMapControl2.Map as IGraphicsContainer; IActiveView pAv = pGra as IActiveView; // 在绘制前,清除 axMapControl2 中的任何图形元素 pGra.DeleteAllElements(); IRectangleElement pRectangleEle = new RectangleElement() as IRectangleElement; IElement pEle = pRectangleEle as IElement; pEle.Geometry = pEnv; // 设置鹰眼图中的红线框 IRgbColor pColor = new RgbColor(); pColor.Red = 255; pColor.Green = 0; pColor.Blue = 0; pColor.Transparency = 255; // 产生一个线符号对象 ILineSymbol pOutline = new SimpleLineSymbol(); pOutline.Width = 2; pOutline.Color = pColor; // 设置颜色属性 pColor = new RgbColor(); pColor.Red = 255; pColor.Green = 0; pColor.Blue = 0; pColor.Transparency = 0; // 设置填充符号的属性 IFillSymbol pFillSymbol = new SimpleFillSymbol(); pFillSymbol.Color = pColor; pFillSymbol.Outline = pOutline; IFillShapeElement pFillShapeEle = pEle as IFillShapeElement; pFillShapeEle.Symbol = pFillSymbol; pGra.AddElement((IElement)pFillShapeEle, 0); // 刷新 pAv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
三、鹰眼与主 Map 控件互动:
private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e){ if (this.axMapControl2.Map.LayerCount != 0) { // 按下鼠标左键移动矩形框 if (e.button == 1) { IPoint pPoint = new ESRI.ArcGIS.Geometry.Point(); pPoint.PutCoords(e.mapX, e.mapY); IEnvelope pEnvelope = this.axMapControl1.Extent; pEnvelope.CenterAt(pPoint); this.axMapControl1.Extent = pEnvelope; this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } // 按下鼠标右键绘制矩形框 else if (e.button == 2) { IEnvelope pEnvelop = this.axMapControl2.TrackRectangle(); this.axMapControl1.Extent = pEnvelop; this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } } }
为鹰眼控件 MapControl2 添加 OnMouseMove 事件,主要实现按下鼠标左键的时候移动矩形框,同时也改变主的图控件的显示范围。代码如下:
private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e){ // 如果不是左键按下就直接返回 if (e.button != 1) return; IPoint pPoint = new ESRI.ArcGIS.Geometry.Point(); pPoint.PutCoords(e.mapX, e.mapY); this.axMapControl1.CenterAt(pPoint); this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); }
效果:
详情参见:
http://www.gissky.net/Article/1558.htm