二、AE+C#之地图图层基本操作
2019-01-30 本文已影响6人
平凡的鱼仔
打开vs2012,新建一个项目,命名为MapControlApp1,如图:
012901.PNG
012902.PNG
双击MainForm.cs,变成下面的样子:
012903.PNG
若想将菜单改为中文,可以在属性窗口的Text处修改,和VB类似:
012903.PNG
出现下面的画面:
012905.png
依次修改file菜单下的子菜单,全部修改完后:
012906.png
增加命令菜单‘加载图层’:
012907.png
,依次添加:
012908.PNG
其实这样做是为了熟悉加载图层的方法,在文件下的‘打开’命令菜单可以达到同样的效果。修改‘通过AddLayer加载’的name,随便改:
012910.png
其它两个也顺便改了,我分别改为了AddLayerFromFileLoad,AddShapeFileLoad。双击‘通过AddLayer加载’跳出代码区,键入下列代码:
012911.PNG
012911.PNG
012912.PNG
在‘加载图层’命令菜单下添加‘对话框形式加载’,然后添加三个子菜单‘加载layer图层’、‘加载ShapeFile’、‘加载地图文档’:
012913.PNG
双击‘加载layer图层’,增加代码:
012914.PNG
012911.PNG
012915.PNG
012916.PNG
012917.PNG
012918.PNG
![](https://img.haomeiwen.com/i13897358/e2440746e277cab2.png)
![](https://img.haomeiwen.com/i13897358/34fddfc55c8f82ad.png)
![](https://img.haomeiwen.com/i13897358/238b320871c3695e.png)
![](https://img.haomeiwen.com/i13897358/2880897236cf3a43.png)
private void AddLayerLoad_Click(object sender, EventArgs e)
{
//此处的路径改为自己电脑上Continents.lyr的位置
String lyrPath = @"D:\Program Files (x86)\ArcGIS\DeveloperKit10.2\Samples\data\World\Continents.lyr";
MapDocument mapDoc = new MapDocument();
mapDoc.Open(lyrPath);
//获取第一个地图的第一个图层
ILayer pLyr = mapDoc.get_Layer(0, 0);
axMapControl1.AddLayer(pLyr);
}
调试运行,出现下列画面:
双击‘通过AddLayerFromFile加载’,增加代码:
private void AddLayerFromFileLoad_Click(object sender, EventArgs e)
{
string lyrPath = @"D:\Program Files (x86)\ArcGIS\DeveloperKit10.2\Samples\data\World\Continents.lyr";
axMapControl1.AddLayerFromFile(lyrPath);
}
调试运行后,点击‘通过AddLayerFromFile加载’,画面如下,和上一个做法效果一样:
双击‘通过AddShapeFile加载’,增加代码:
private void AddShapeFileLoad_Click(object sender, EventArgs e)
{
string shpPath = @"D:\Program Files (x86)\ArcGIS\DeveloperKit10.2\Samples\data\World";
axMapControl1.AddShapeFile(shpPath, "World30.shp");
}
调试运行后,点击‘通过AddShapeFile加载’,结果如下:
private void DialogAddLayer_Click(object sender, EventArgs e)
{
OpenFileDialog opFDialog = new OpenFileDialog();
opFDialog.Title = "请选择图层";
opFDialog.Filter = "layers(*.lyr)|*.lyr";
if (opFDialog.ShowDialog() == DialogResult.OK)
{
string lyrPath = opFDialog.FileName;
axMapControl1.AddLayerFromFile(lyrPath);
}
}
双击‘加载ShapeFile’,增加代码:
private void DialogAddShapeFile_Click(object sender, EventArgs e)
{
OpenFileDialog opFDialog = new OpenFileDialog();
opFDialog.Title = "请选择Shapefile";
opFDialog.Filter = "Shapefile(*.shp)|*.shp";
if (opFDialog.ShowDialog() == DialogResult.OK)
{
string shpPath = opFDialog.FileName;
string filePath = System.IO.Path.GetDirectoryName(shpPath);
string fileName = System.IO.Path.GetFileNameWithoutExtension(shpPath);
axMapControl1.AddShapeFile(filePath, fileName);
}
双击‘加载地图文档’,增加代码:
private void DialogAddmxd_Click(object sender, EventArgs e)
{
OpenFileDialog opFDialog = new OpenFileDialog();
opFDialog.Title = "请选择地图文档";
//'|'前的文字是选择对话框的提示文字,后面是格式(后缀)过滤,每个格式用';'分开
opFDialog.Filter = "Map Document(*.mxd;*.pmf;*.mxt)|*.mxd;*.pmf;*.mxt";
if (opFDialog.ShowDialog() == DialogResult.OK)
{
string mxPath = opFDialog.FileName;
axMapControl1.LoadMxFile(mxPath);
}
}
运行调试,依次点击‘加载layer图层’、‘加载ShapeFile’、‘加载地图文档’,结果依次为:
上一节:一、AE+C#之可视化组件