《ArcGIS Engine开发 从入门到精通》学习笔记1 地图的加载与保存。

照着书上敲完了两个主要函数的代码,先mark一下,以备下次偷懒。

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;

private void LoadMapDocument()
{
System.Windows.Forms.OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map documents (*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filePath + "不是有效的地图文档!");
}
}

private void LoadMapDocument2()
{
System.Windows.Forms.OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map documents (*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
ESRI.ArcGIS.esriSystem.IArray arrayMap = axMapControl1.ReadMxMaps(filePath, Type.Missing);
ESRI.ArcGIS.Carto.IMap map;
for (int i = 0; i < arrayMap.Count; i++)
{
map = arrayMap.get_Element(i) as ESRI.ArcGIS.Carto.IMap;
if (map.Name == "Layers")
{
axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(filePath + "不是有效的地图文档!");
}
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
LoadMapDocument();
}

private void button2_Click(object sender, EventArgs e)
{
LoadMapDocument2();
}

敲完代码之后报错:

error CS0246: 未能找到类型或命名空间名称“IArray”(是否缺少 using 指令或程序集引用?)

 error CS0246: 未能找到类型或命名空间名称“IMap”(是否缺少 using 指令或程序集引用?)

 

百度了一下,是缺少类库引用,那么问题来了,明明引用中已经添加了这个,还是报错、

然后又积(wu)极(chi)的百度了一下,原来C#的引用文件夹与使用using的区别是大大的!

引用文件夹引用的是目标文件:当程序需要使用外部库的时候,引用文件夹向编译器说明:我正在使用外部类库,并且告之这个外部类库的位置。

using引用的是目标内容:这个才是我们正常遇到的添加引用,不对,准确的说应该是使用这个命名空间,这样的话,就可以直接调用我想要的内容。

举例来说:

我是用的IArray和IMap是包含在ESRI.ArcGIS.esriSystem和ESRI.ArcGIS.Carto里面的。

我添加了引用文件夹表示我现在就可以使用这两个类库了,同样的我也就可以使用这两个变量类型了。

但是,现实是当我使用它们的时候还是报错,IArray和IMap没有声明。

原因就是,虽然我告诉了编译器,我要是用这两个类库,但是编译器只认识这两个类库,并不认识IArray和IMap。

所以我就需要添加using ESRI.ArcGIS.esriSystem 和using ESRI.ArcGIS.Carto,这样的话我就可以使用它们了。

当然不引用这两个类库也是可以的,在使用的时候,ESRI.ArcGIS.esriSystem.IArray和ESRI.ArcGIS.Carto.Imap就可以了。

这个不用说明了吧,std::cout<< "hello world!"<<std::endl;想必大家都用过!

最后展示一下效果。

最后再唠叨一句,那个第二个加载函数的代码我是照着书上抄的,发现并不能实现效果,我也看不懂这段代码是要干什么,暂时用不上,所以弃之不理!

采用IMapDocument类型进行地图文件的加载,保存和另存为。

namespace 地图的保存
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

IMapDocument mapDoc;
private void loadMapDoc()
{
mapDoc = new MapDocumentClass();
try
{
System.Windows.Forms.OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map documents (*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
mapDoc.Open(filePath, "");
for (int i = 0; i < mapDoc.MapCount; i++)
{
axMapControl1.Map = mapDoc.get_Map(i);
}
axMapControl1.Refresh();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

private void saveDoc()
{
if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
{
MessageBox.Show("地图文档只读,无法保存!");
}
else
{
try
{
mapDoc.Save(mapDoc.UsesRelativePaths, true);
MessageBox.Show("保存地图文档成功!");
}
catch (Exception e)
{
MessageBox.Show("保存地图文档失败" + e.ToString());
}
}
}

private void saveAsDoc()
{
if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
{
MessageBox.Show("地图文档只读,无法保存!");
}
else
{
try
{
System.Windows.Forms.SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "保存地图文档";
saveFileDialog.FileName = "new.mxd";
saveFileDialog.Filter = "map documents (*.mxd)|*.mxd";
saveFileDialog.ShowDialog();
string filePath = saveFileDialog.FileName;
mapDoc.SaveAs(filePath, true, true);
MessageBox.Show("保存地图文档成功!");
}
catch (Exception e)
{
MessageBox.Show("保存地图文档失败" + e.ToString());
}
}
}
private void button1_Click(object sender, EventArgs e)
{
loadMapDoc();
}

private void button2_Click(object sender, EventArgs e)
{
saveDoc();
}

private void button3_Click(object sender, EventArgs e)
{
saveAsDoc();
}

编译之后发现报错。

IMapDocument m_MapDocument = new ESRI.ArcGIS.Carto.MapDocumentClass();

报错:

无法嵌入互操作类型"ESRI.ArcGIS.Carto.MapDocumentClass".请改用适用的接口.

 

解决方案如下:

解决方案——项目——引用——ESRI.ArcGIS.Carto

右键ESRI.ArcGIS.Carto ——属性——嵌入互操作类型=false

转自:http://theseaanna.blog.163.com/blog/static/1997180092011101984414974/

 

posted @ 2015-12-14 16:03  Zzitai  阅读(3988)  评论(0编辑  收藏  举报