ArcGIS二次开发基础教程(02):地图导航和鹰眼
地图导航(主要是调用命令和工具)
- 地图的放缩和漫游
if(axMapControl1.CurrentTool == null)
{ICommand icc;//地图放大ITool tool = new ControlsMapZoomInToolClass();//地图缩小//ITool tool = new ControlsMapZoomOutToolClass();//地图漫游//ITool tool = new ControlsMapPanToolClass();icc = tool as ICommand;icc.OnCreate(axMapControl1.Object);//钩子函数icc.OnClick();
}
-
全局地图
ICommand icc = new ControlsFullExtentCommandClass(); icc.OnCreate(axMapControl1.Object); icc.OnClick();
鹰眼
//注意:只有从mxd文件中加载图层鹰眼才会出现 axMapControl1是主地图 axMapControl2为鹰眼图
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{axMapControl2.Map.ClearLayer();if(axMapControl1.LayerCount>0){//从下往上获取图层添加到鹰眼图中防止覆盖for(int i= axMapControl1.LayerCount-1;i>=0;i--){ILayer layer = axMapControl1.get_Layer(i);IObjectCopy copy = new ObjectCopyClass();ILayer myLayer = copy.Copy(layer) as ILayer;axMapControl2.AddLayer(layer);}//设置相同的空间参考axMapControl2.SpatialReferencr = axMapControl1.SpatialReference;//鹰眼图的全局为主地图的当前视图axMapControl2.FullExtent = axMapControl1.Extent;//每次主地图图层增减都刷新axMapControl2.Refresh();}
}
//当主地图变化时,鹰眼图框变化
private void axMapControl2_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{//创建一个矩形元素并转换为元素IElement ele = new RectangleElementClass() as IElement;//获取主地图的视图IEenvlop env = axMapControl1.Extent;ele.Geometry = env as IGeometry;//外轮廓线ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();IRGBColor color1 = new RGBColorClass();color1.Red = 255;color1.Green = 0;color1.Blue = 0;//透明度为255即不透明color1.Tranparency = 255;lineSymbol.Color= color1;lineSymbol.Width = 2;ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();IRGBColor color2 = new RGBColorClass();color2.Red = 255;color2.Green = 0;color2.Blue = 0;//透明度为0即完全透明 color2.Tranparency = 0;fillSymbol.Color = color2;fillSymbol.Outline = lineSymbol;//实现线框的生成IFillShapeElement fillShapeElement = ele as IFillShapeElement;fillShapeElement.Symbol = fillSymbol;//图形容器IGraphicsContainer graphicsContainer = axMapControl2.Map as IGraphicsContainer;//删掉以前的元素graphicsContainer.DeleteAllElements();//添加新元素graphicsContainer.AddElement(fillShapeElement as IElement);axMapControl2.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
//鹰眼图的鼠标点击事件private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e){if(e.Button == 1){//将主地图视图中心移到点击处IPoint point = new PointClass();point.PutCoords(e.mapX,e.mapY);axMapControl1.CenterAt(point);axMapControl1.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);}else{//将主地图视图变为从鹰眼图获取的轨迹矩形IEnvelop env = axMapControl2.TrackRectangle;axMapCOntrol1.Extent = env;axMapControl1.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);}}
//鹰眼图的鼠标移动事件private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e){if(e.Button == 1){IPoint point = new PointClass();point.PutCoords(e.mapX,e.mapY);axMapControl1.CenterAt(point);axMapControl1.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);}}
鹰眼效果:
历届GIS应用技能大赛开发题答案点这里,尚在不定期更新中