二、Arcgis engine-MapControl控件(上)

MapControl对应于ArcMap中的数据视图,它封装了Map对象,并提供了额外的属性、事件、方法用于:

  • 管理控件的外观、显示属性和地图属性;
  • 添加并管理控件中的数据层(data layers);
  • 装载Map文档(mxd)到控件中;
  • 从其它应用程序拖放数据到控件中;
  • 使用鼠标拉绘几何形状并绘制到屏幕显示中。
    MapControl实现的常用接口为IMapControlDefault(缺省接口,开发过程中使用该接口可保证总是访问最新版本的MapControl)和IMapControl4。

加载地图

  • [1] 无代码,直接利用控件加载
    在窗体中添加AxToolbarControl、AxTOCControl和AxMapControl三个控件,有些时候不添加AxLienceControl控件运行会报错,所以也可添加一个Lience控件。在这里插入图片描述
    ToolbarControl控件相当于ArcMap中的工具条,axTOCCoontrol控件则相当于ArcMap中左侧的布局视图,axMapControl控件则相当于ArcMap中的数据视图。右键axToolbarControl属性将Genneral选项卡中的Buddy,选择axMapControl1即与当前的axMapControl控件绑定起来,继续选中items,Add所需的工具到工具条中,如下图所示。在这里插入图片描述
    同理,将左侧axTOCControl属性中的Buddy绑定axMapControl1。运行程序结果如下:
    在这里插入图片描述
    • [2] 使用LoadMxFile加载
      代码如下:
	    System.Windows.Forms.OpenFileDialog openFileDialog1 = new OpenFileDialog();//新建一个openFileDialog实例
            openFileDialog1.Title = "Open Map Document";//设置选择框的名称
            openFileDialog1.Filter = "Map Documents(*.mxd)|*.mxd";//设置文件类型
            openFileDialog1.ShowDialog();
            string sFilePath = openFileDialog1.FileName;//存储文件路径
            if (axMapControl1.CheckMxFile(sFilePath))
            {
                axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;//MousePointer该值指示在运行时当du鼠标移动到对象的一个特定部分时,被显示的鼠标指针的类型。
                axMapControl1.LoadMxFile(sFilePath, 0, Type.Missing);//如果选中的文档为合法文档,则调用LoadMxFile方法加载
                axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
            }
            else
            {
                MessageBox.Show(sFilePath + "is not a valid ArcMap document");
                return;
            }

在这里插入图片描述

  • [3] 使用IMapDocument接口加载
    代码如下:
 	    IMapDocument m_MapDocument = new MapDocumentClass();
            System.Windows.Forms.OpenFileDialog openFileDialog2 = new OpenFileDialog();
            openFileDialog2.Title = "Open Map Document";//设置选择框的名称
            openFileDialog2.Filter = "Map Documents(*.mxd)|*.mxd";//设置文件类型
            openFileDialog2.ShowDialog();
            string sFilePath = openFileDialog2.FileName;//存储文件路径
            if (m_MapDocument.IsMapDocument[sFilePath])//判断文件类型
            {
                m_MapDocument.Open(sFilePath, "");//加载地图文档
                axMapControl1.Map = m_MapDocument.Map[0];//加载第一个地图,如需加载多个地图需要循环。
                axMapControl1.Refresh();//刷新。
            }

在这里插入图片描述
一些错误点:

  1. m_MapDocument.IsMapDocument(sFilePath)处报错
    可调用的成员“IMapDocument.IsMapDocument[string]”不能像方法一样使用。
    改为m_MapDocument.IsMapDocument[sFilePath]即可,
    原因:在C#中带有()的一般都作为方法,所以就会报错。

2.利用IMapDocument获取地图有两种写法:
axMapControl1.Map = m_MapDocument.get_Map(0);
axMapControl1.Map = m_MapDocument.Map[0];

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值