C#+AE中MapControl加载进来的地图不在控件的正中间,向右偏了一点,这是为什么呢??如何
C#+AE中MapControl加载进来的地图不在控件的正中间,向右偏了一点,这是为什么呢??如何调整过来???...
C#+AE中MapControl加载进来的地图不在控件的正中间,向右偏了一点,这是为什么呢??如何调整过来???
展开
1个回答
2016-01-21
展开全部
整理了 MapConrol各基本功能的实现代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
namespace MapCtrol //直接引用时需替换成自己当前的命名空间
{
public class MapBaseOperate
{
/// <summary>
/// 添加SHP文当
/// </summary>
/// <param name="mapControl"></param>
public static void AddShapeFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加shp图层文件";
openfileDlg.Filter = "map document (*.shp)|*.shp";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路径不存在!");
return;
}
string path;
string filename;
//int istart = filepath.LastIndexOf("\\");
//int iend = filepath.LastIndexOf(".");
//path = filepath.Substring(0, istart);
//filename = filepath.Substring(istart + 1, iend - istart - 1);
FileInfo fileinfo = new FileInfo(filepath);
path = filepath.Substring(0, filepath.Length - fileinfo.Name.Length);
filename = fileinfo.Name;
try
{
//加载图层文件
mapControl.AddShapeFile(path, filename);
//设置MapControl的显示范围到数据的全局范围
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加图层文件失败!" + ex.Message);
}
}
/// <summary>
/// 添加LYR文当
/// </summary>
/// <param name="mapControl"></param>
public static void AddLayerFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加lyr图层文件";
openfileDlg.Filter = "map documents (*.lyr)|*.lyr";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路径不存在!");
return;
}
try
{
mapControl.AddLayerFromFile(filepath);
//设置MapControl的显示范围到数据的全局范围
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加图层文件失败!" + ex.Message);
}
}
/// <summary>
/// 删除地图所有图层
/// </summary>
public static void DeleteAllLayers(IMapControlDefault mapControl)
{
try
{
for (int i = mapControl.LayerCount - 1; i >= 0; i-- )
{
mapControl.DeleteLayer(i);
}
}
catch (System.Exception ex)
{
MessageBox.Show("删除图层失败!" + ex.Message);
}
}
/// <summary>
/// 将最底图层,移动到最上层
/// </summary>
public static void MoveLayerToTop(IMapControlDefault mapControl)
{
try
{
if (mapControl.LayerCount > 0)
{
mapControl.MoveLayerTo(mapControl.LayerCount - 1, 0);
}
}
catch (System.Exception ex)
{
MessageBox.Show("移动图层失败!" + ex.Message);
}
}
/// <summary>
/// 加载地图文当
/// </summary>
/// <param name="mapControl"></param>
public static void LoadMapDocument(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加载地图文当";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filepath + "不是有效的地图文当!");
}
}
/// <summary>
/// 加载特定地图文当
/// </summary>
/// <param name="mapControl"></param>
public static void LoadSpecificMapDocument(IMapControlDefault mapControl, string specificMapName)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加载特定地图文当";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
if (string.IsNullOrWhiteSpace(specificMapName))
{
int istart = filepath.LastIndexOf("\\");
int iend = filepath.LastIndexOf(".");
specificMapName = filepath.Substring(istart + 1, iend - istart - 1);
}
IArray arrayMap = mapControl.ReadMxMaps(filepath, Type.Missing);
for (int i = 0; i < arrayMap.Count; i++)
{
IMap map = arrayMap.get_Element(i) as IMap;
if (specificMapName == map.Name)
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(filepath + "不是有效的地图文当!");
}
}
/// <summary>
/// By MapDocument
/// </summary>
public static IMapDocument LoadMapDoc(IMapControlDefault mapControl)
{
MapDocument mapdoc = new MapDocument();
try
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加载地图文当";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
mapdoc.Open(filepath, "");
for (int i = 0; i < mapdoc.MapCount; i++ )
{
mapControl.Map = mapdoc.get_Map(i);
}
mapControl.Refresh();
}
catch (System.Exception ex)
{
MessageBox.Show("加载地图文当失败" + ex.Message);
mapdoc = null;
}
return mapdoc;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
namespace MapCtrol //直接引用时需替换成自己当前的命名空间
{
public class MapBaseOperate
{
/// <summary>
/// 添加SHP文当
/// </summary>
/// <param name="mapControl"></param>
public static void AddShapeFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加shp图层文件";
openfileDlg.Filter = "map document (*.shp)|*.shp";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路径不存在!");
return;
}
string path;
string filename;
//int istart = filepath.LastIndexOf("\\");
//int iend = filepath.LastIndexOf(".");
//path = filepath.Substring(0, istart);
//filename = filepath.Substring(istart + 1, iend - istart - 1);
FileInfo fileinfo = new FileInfo(filepath);
path = filepath.Substring(0, filepath.Length - fileinfo.Name.Length);
filename = fileinfo.Name;
try
{
//加载图层文件
mapControl.AddShapeFile(path, filename);
//设置MapControl的显示范围到数据的全局范围
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加图层文件失败!" + ex.Message);
}
}
/// <summary>
/// 添加LYR文当
/// </summary>
/// <param name="mapControl"></param>
public static void AddLayerFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加lyr图层文件";
openfileDlg.Filter = "map documents (*.lyr)|*.lyr";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路径不存在!");
return;
}
try
{
mapControl.AddLayerFromFile(filepath);
//设置MapControl的显示范围到数据的全局范围
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加图层文件失败!" + ex.Message);
}
}
/// <summary>
/// 删除地图所有图层
/// </summary>
public static void DeleteAllLayers(IMapControlDefault mapControl)
{
try
{
for (int i = mapControl.LayerCount - 1; i >= 0; i-- )
{
mapControl.DeleteLayer(i);
}
}
catch (System.Exception ex)
{
MessageBox.Show("删除图层失败!" + ex.Message);
}
}
/// <summary>
/// 将最底图层,移动到最上层
/// </summary>
public static void MoveLayerToTop(IMapControlDefault mapControl)
{
try
{
if (mapControl.LayerCount > 0)
{
mapControl.MoveLayerTo(mapControl.LayerCount - 1, 0);
}
}
catch (System.Exception ex)
{
MessageBox.Show("移动图层失败!" + ex.Message);
}
}
/// <summary>
/// 加载地图文当
/// </summary>
/// <param name="mapControl"></param>
public static void LoadMapDocument(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加载地图文当";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filepath + "不是有效的地图文当!");
}
}
/// <summary>
/// 加载特定地图文当
/// </summary>
/// <param name="mapControl"></param>
public static void LoadSpecificMapDocument(IMapControlDefault mapControl, string specificMapName)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加载特定地图文当";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
if (string.IsNullOrWhiteSpace(specificMapName))
{
int istart = filepath.LastIndexOf("\\");
int iend = filepath.LastIndexOf(".");
specificMapName = filepath.Substring(istart + 1, iend - istart - 1);
}
IArray arrayMap = mapControl.ReadMxMaps(filepath, Type.Missing);
for (int i = 0; i < arrayMap.Count; i++)
{
IMap map = arrayMap.get_Element(i) as IMap;
if (specificMapName == map.Name)
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(filepath + "不是有效的地图文当!");
}
}
/// <summary>
/// By MapDocument
/// </summary>
public static IMapDocument LoadMapDoc(IMapControlDefault mapControl)
{
MapDocument mapdoc = new MapDocument();
try
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加载地图文当";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
mapdoc.Open(filepath, "");
for (int i = 0; i < mapdoc.MapCount; i++ )
{
mapControl.Map = mapdoc.get_Map(i);
}
mapControl.Refresh();
}
catch (System.Exception ex)
{
MessageBox.Show("加载地图文当失败" + ex.Message);
mapdoc = null;
}
return mapdoc;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询