1. 程式人生 > >ArcGIS Engine 系統開發設計(二):開啟地圖文件、鷹眼圖的製作

ArcGIS Engine 系統開發設計(二):開啟地圖文件、鷹眼圖的製作


首先是製作一個按鈕來負責開啟地圖文件:

在toolbox中選擇Button控制元件拖入我們的Form中,接下來在該button的Cilck事件中呼叫 OpenFileDialog類獲取檔案路徑後,

 將檔案路徑呼叫到axMapControl1.LoadMxFile(path)中就可以開啟MXD文件了。

        private void button1_Click(object sender, EventArgs e)
        {
      OpenFileDialog OpenMXD = new OpenFileDialog();
      OpenMXD.Title = "開啟地圖";
      OpenMXD.InitialDirectory = "E:";
      OpenMXD.Filter = "Map Documents (*.mxd)|*.mxd";
      if (OpenMXD.ShowDialog() == DialogResult.OK)
      {
      string MxdPath = OpenMXD.FileName;
      axMapControl1.LoadMxFile(MxdPath);
<span style="white-space:pre">	</span>}
        }

我們可以通過相同的方法開啟shape檔案,但是這裡要注意:

axMapControl1.AddShapeFile()方法中,並不是像LoadMx一樣直接輸入檔案路徑就行,而是AddShapeFile(filePath, fileName),因此我們要先編寫一個函式將檔案路徑的字串進行分割:

        private void button2_Click(object sender, EventArgs e)
        {
            {
                string[] S = OpenShapeFile();
                try
                {
                    axMapControl1.AddShapeFile(S[0], S[1]);
                }
                catch
                {
                    MessageBox.Show("請至少選擇一個shape檔案", "ERROR");
                
                  }

                }
            }
  

        public string[] OpenShapeFile()
        {
            string[] ShpFile = new string[2];
            OpenFileDialog OpenShpFile = new OpenFileDialog();
            OpenShpFile.Title = "開啟Shape檔案";
            OpenShpFile.InitialDirectory = "E:";
            OpenShpFile.Filter = "Shape檔案(*.shp)|*.shp";
            if (OpenShpFile.ShowDialog() == DialogResult.OK)
            {
                string ShapPath = OpenShpFile.FileName;
                //利用"\\"將檔案路徑分成兩部分
                int Position = ShapPath.LastIndexOf("\\");
                string FilePath = ShapPath.Substring(0, Position);
                string ShpName = ShapPath.Substring(Position + 1);
                ShpFile[0] = FilePath;
                ShpFile[1] = ShpName;
                
            }
            return ShpFile;
        }


執行後結果如下:



這部分完成後,接下來是鷹眼圖的製作~:


鷹眼圖的操作主要分為兩個部分,當在主控制元件中重新載入一幅圖的時候,另外一個控制元件的圖也發生相應的變化, 大致思路是在獲得你在開啟主地圖後,向鷹眼圖(MapControl2)中新增相同的圖層,並不斷更新你在主地圖的當前範圍,再在鷹眼圖的對應區域中繪製一個紅框表示對應範圍。

這裡主要使用了IEnvelope和IPoint介面,用來獲取滑鼠所在座標、繪製表示範圍的紅框,具體用法可以參考這裡~


我們在form中拖入第二個地圖控制元件axMapControl2,用它作為axMapControl1的鷹眼圖進行表示。

這裡首先對MapControl1的OnMapReplaced事件和OnExtentUpdated事件進行編寫,讓我們獲得MapControl1的地圖範圍更新,並向MapControl2新增圖層、繪製矩形:


        private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            //設定一個新的外接矩形
            IEnvelope pEnvelope = (IEnvelope)e.newEnvelope;
            IGraphicsContainer pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
            IActiveView pActiveView = pGraphicsContainer as IActiveView;
            //在繪製前,清除axMapControl2中的任何圖形元素
            pGraphicsContainer.DeleteAllElements();
            IRectangleElement pRectangleEle = new RectangleElementClass();
            IElement pElement = pRectangleEle as IElement;
            pElement.Geometry = pEnvelope;
            //設定鷹眼圖中的紅線框
            IRgbColor pColor = new RgbColorClass();
            pColor.Red = 255;
            pColor.Green = 0;
            pColor.Blue = 0;
            pColor.Transparency = 255;
            //產生一個線符號物件
            ILineSymbol pOutline = new SimpleLineSymbolClass();
            pOutline.Width = 3;
            pOutline.Color = pColor;
            //設定顏色屬性
            pColor = new RgbColorClass();
            pColor.Red = 255;
            pColor.Green = 0;
            pColor.Blue = 0;
            pColor.Transparency = 0;
            //設定填充符號的屬性
            IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
            pFillSymbol.Color = pColor;
            pFillSymbol.Outline = pOutline;
            IFillShapeElement pFillShapeEle = pElement as IFillShapeElement;
            pFillShapeEle.Symbol = pFillSymbol;
            pGraphicsContainer.AddElement((IElement)pFillShapeEle, 0);
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            //將地圖範圍顯示在StripStatus中
            IPoint ll, Ur;
            ll = axMapControl1.Extent.LowerLeft;
            Ur = axMapControl1.Extent.LowerRight;
            toolStripStatusLabel3.Text = "(" + Convert.ToString(ll.X) + "," + Convert.ToString(ll.Y) + ")";

        }

        private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
        {                                                                                                                                                             //向MapControl2新增圖層
            if (axMapControl1.LayerCount > 0)
            {
                axMapControl2.Map = new MapClass();
                for (int i = 0; i <= axMapControl1.Map.LayerCount - 1; i++)
                {
                    
                    axMapControl2.AddLayer(axMapControl1.get_Layer(i));
                }
                axMapControl2.Extent = axMapControl1.Extent;
                axMapControl2.Refresh();

            }
        }
接下來就是對MapControl2控制元件的On_MouseDown 和 On_MouseMove事件進行編寫,這樣可以讓我們通過拖動鷹眼圖上的紅框反向操作MapControl1中的地圖位置:

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

        private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            if (axMapControl2.Map.LayerCount > 0)
            {
                if (e.button == 1)
                {
                    IPoint pPoint = new PointClass();                                                                                                                         //將點選位置的座標轉換後設為MapControl1的中心
                    pPoint.PutCoords(e.mapX, e.mapY);
                    axMapControl1.CenterAt(pPoint);
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }
                else if (e.button == 2)
                {
                    IEnvelope pEnv = axMapControl2.TrackRectangle();
                    axMapControl1.Extent = pEnv;
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }
            }

        }

最後在Form左下角再新增一個statusStrip控制元件,就可以實時顯示當前圖幅的範圍了~

最終效果如下: