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;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200715164502877.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNDA0NjM1,size_16,color_FFFFFF,t_70)
[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();//刷新。
}
一些错误点:
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];
|