照着書上敲完了兩個主要函數的代碼,先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/