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

1 管理控件的外观、显示属性和地图属性

2 添加并管理控件中的数据层

3 装载Map文档控件中

4 从其它应用程序拖放数据到控件中

5 tracking shapes and drawing to the display

MapControl实现的主要接口有:IMapControlDefault   IMapControl2 IMapControl3和事件接口IMapControlEvents2

IMapControlDefault接口是地图控件缺省接口,多数开发环境自动使用这个接口定义的属性和方法。由于MapControl是一个自动化控件,当它被放到一个容器---如窗体上后,它会自动产生一个被称为axMapControl1的对象,这个对象可以直接使用缺省接口定义的属性和方法。MapControl当前最新版本接口为IMapControl3.

当需要使用这个接口时,可使用下面的代码:

IMapControlDefault pMapControl;
pMapControl = axMapControl1.Object as IMapControlDefault;

对于文档文件,MapControl控件可以直接使用LoadMxFile方法来载入,这是最简单的方法。除此之外,也可以使用IMapDocument接口定义的属性和方法来加载一个MSD文件。下面是一个载入文档的例子:

private void LoadMapDocument()
{
System.Windows.Forms.OpenFileDialog openFileDialog2;
openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "Open Map Document";
openFileDialog2.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
if (axMapControl1.CheckMxFile(sFilePath))
{
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(sFilePath, 0,Type.Missing);
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(sFilePath + " is not a valid ArcMap document");
return;
}
}

上例中,先使用.NET框架类库提供的OpenFileDialog对话框找到要打开的MSD文档sFilePath,然后利用AxMapControl提供的方法LoadMxFile()打开该MXD文档,否则,显示有关信息。

如果要装载某个地图文档中的特定地图,则可以使用以下方法:

private void LoadMapDocument2()
{
System.Windows.Forms.OpenFileDialog openFileDialog1;
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open Map Document";

openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog1.ShowDialog();
string sFilePath = openFileDialog1.FileName;
if (axMapControl1.CheckMxFile(sFilePath))
{
IArraypArray ;
pArray = axMapControl1.ReadMxMaps(sFilePath,Type.Missing);
int i ;
IMap pMap;
//遍历这些Map对象
for(i = 0;i<=pArray.Count - 1;i++)
{
//QI
pMap = pArray.get_Element(i) as IMap;
if (pMap.Name == "Layers")
{
//载入文档中特定的Map对象
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(sFilePath, 0,Type.Missing);
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(sFilePath + " is not a valid ArcMap document");
return;
}
}

下面的例子是打开、保存、另存为一个文档文件,详细代码如下:

IMapDocument m_MapDocument;
private void LoadMapDoc()
{
m_MapDocument = new MapDocumentClass();
try
{
//打开文件对话框选取MXD文件
System.Windows.Forms.OpenFileDialog openFileDialog2;
openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "Open Map Document";
openFileDialog2.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
//将数据载入pMapDocument并与map控件联系起来
m_MapDocument.Open(sFilePath, "");
int i;
for (i = 0 ; i<= pMapDocument.MapCount - 1;i++)
{
//一个IMapDocument对象中可能有多个Map对象,遍历每个map对象
axMapControl1.Map = m_MapDocument.get_Map(i);
}
//刷新地图
axMapControl1.Refresh();
}
catch( Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void SaveDocument()
{

//Check that the document is not read only
if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename)
== true)
{
MessageBox.Show("This map document is read only!");
return;
}
//Save with the current relative path setting
m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true);
MessageBox.Show("Changes saved successfully!");
}
private void SaveAsDocument ()
{
//Open a file dialog for saving map documents
saveFileDialog1.Title = "Save Map Document As";
saveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
saveFileDialog1.ShowDialog();
//Exit if no map document is selected
string sFilePath = saveFileDialog1.FileName;
if (sFilePath == "")
{
return;
}
if (sFilePath == m_MapDocument.DocumentFilename)
{
//Save changes to the current document
SaveDocument();
}
else
{
//SaveAs a new document with relative paths
m_MapDocument.SaveAs(sFilePath, true, true);
//Open document
OpenDocument((sFilePath));
MessageBox.Show("Document saved successfully!");

}
}
新建一个空的地图文档,并将其加载到 MapControl 的代码如下:
private void mnuNew_Click(object sender, System.EventArgs e)
{
// IMap pMap = new MapClass();
// axMapControl1.Map = pMap ;
MapDocument pMapDocument = new MapDocumentClass();
string sFilePath = @"d:\temp\untitled.mxd";
pMapDocument.New(sFilePath);
pMapDocument.Open(sFilePath,"");
axMapControl1.Map = pMapDocument.get_Map(0);
}

此外,MapControl控件还可以使用AddLayerFromFile方法添加一个图层文件,使用AddshapeFile添加一个Shape文件,使用AddLayer快速添加一个图层,DeleteLayer快速删除一个图层,MoveLayerTo改变一个图层的索引等。

05-11 13:29