地图书签,可以理解为暂时记录当前地图的范围和放大级别,在后续的操作中如果想回到地图之前的状态,就可以点击保存的书签就可以回到此状态,如图所示:
地图刚加载的时候是一幅世界地图
我们将地图的中心拖到南美洲,将地图放大到一定比例,创建以“南美洲”为名称的书签,如下图所示:
创建后书签,在地图上随意拖动地图,并进行放大或缩小,然后点击“南美洲”书签,地图会定位到书签的南美洲视图范围。
下面来介绍下实现的主要代码:
要实现地图书签功能主要有两个步骤:
1、如何创建书签;
2、点击书签地图如何定位到书签的范围
一、创建书签
显示书签的列表用的是Devexpress插件中的 LayoutControl控件,
将要保存书签的视图范围输出为图片.png格式,然后用文件流将图片转换成Image,显示在书签列表中。示例中创建的书签信息数据存储在字典表IDictionary<string,object> m_DicBookMarkInfo中,其中值存储形式格式定义的是结构体BookMarkInfo,书签信息数据可以根据数据库的不同进行存储;
/// <summary>
/// 存储书签信息结构体
/// </summary>
public struct BookMarkInfo
{
public string key;//书签名称
public LayoutControlItem layoutControlItem;//书签子控件
public PIE.Geometry.IEnvelope envelope;//书签范围
public PictureBox image;//书签范围图片
public PIE.Geometry.ISpatialReference spatialReference;//空间参考
}
/// <summary>
/// 添加地图书签
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbn_AddBookMark_Click(object sender, EventArgs e)
{
//1、获取书签的名称
FrmAddBookMark bookMark = new FrmAddBookMark(m_DicBookMarkInfo);
if (bookMark.ShowDialog() != DialogResult.OK) return;
m_BookMarkName = bookMark.GetBookMarkName();
if (string.IsNullOrEmpty(m_BookMarkName)) return; //2、输出图片
IDisplayTransformation displayTransformation = mapControlMain.ActiveView.DisplayTransformation;
PIE.Carto.ExportPNG export = new ExportPNG();
export.Width = (int)displayTransformation.DeviceFrame.Width;
export.Height = (int)displayTransformation.DeviceFrame.Height;
export.ExportFileName = @"..\Data\Bookmark\pic\" + m_BookMarkName + ".png"; string filePath = Path.GetDirectoryName(export.ExportFileName);
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
export.StartExporting();
mapControlMain.ActiveView.Output(export as IExport, , displayTransformation.DeviceFrame, mapControlMain.ActiveView.Extent, null);
export.FinishExporting(); //3、界面设计(将图片展示在书签列表)
Design(export.ExportFileName);
}
/// <summary>
/// 书签界面设计
/// </summary>
/// <param name="filePath">输出图片路径</param>
private void Design(string filePath)
{
PictureBox pictureBox1 = new PictureBox();
//将保存的图片展示在书签列表
using (FileStream image = new FileStream(filePath, FileMode.Open))
{
pictureBox1.Image = Image.FromStream(image);
}
LayoutControlItem item = layoutControlGroup1.AddItem(m_BookMarkName, pictureBox1) as LayoutControlItem;
item.Name = m_BookMarkName; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
IEnvelope envelope = m_ActiveView.Extent;
pictureBox1.Name = m_BookMarkName;
pictureBox1.Size = new Size(, );
pictureBox1.Enabled = false; //事件
item.Click += ItemPictureBox_Click;
item.DoubleClick += ItemPictureBox_DoubleClick; item.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
item.MaxSize = new Size(, );
item.MinSize = new Size(, );
item.TextLocation = DevExpress.Utils.Locations.Bottom; item.AppearanceItemCaption.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
item.Spacing = new DevExpress.XtraLayout.Utils.Padding();
item.AppearanceItemCaption.Font = new System.Drawing.Font("宋体", 13F, FontStyle.Bold); //存储标签信息
BookMarkInfo bookMarkInfo = new BookMarkInfo();
bookMarkInfo.key = m_BookMarkName;
bookMarkInfo.layoutControlItem = item;
bookMarkInfo.envelope = envelope;
bookMarkInfo.image = pictureBox1;
bookMarkInfo.spatialReference = mapControlMain.FocusMap.SpatialReference; m_DicBookMarkInfo.Add(m_BookMarkName, bookMarkInfo);//将信息存进字典表中
}
二、点击书签地图定位书签的范围
点击书签分为单击和双击,单击时选中书签,双击时,地图范围更新变化,单击时:选中书签,其他选中的书签设置为不选中
/// <summary>
/// 单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ItemPictureBox_Click(object sender, EventArgs e)
{
LayoutControlItem item = (LayoutControlItem)sender;
m_SelectedBookMarkName = item.Name; item.Selected = true;
foreach (String key in m_DicBookMarkInfo.Keys)
{
if (key != item.Name)
{
BookMarkInfo structBookMark = new BookMarkInfo();
structBookMark = (BookMarkInfo)m_DicBookMarkInfo[key]; LayoutControlItem otherItem = structBookMark.layoutControlItem;
otherItem.Selected = false;
}
}
}
双击时:获取存储的选中对应书签的范围,并根据地图坐标系的不同进行转换,然后将地图的视图更新为选中书签的范围
/// <summary>
/// 双击标签图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ItemPictureBox_DoubleClick(object sender, EventArgs e)
{
LayoutControlItem pb = (LayoutControlItem)sender;
pan(pb);
}
/// <summary>
/// 定位函数
/// </summary>
/// <param name="pb">书签控件</param>
private void pan(LayoutControlItem pb)
{
BookMarkInfo structBookMarkInfo = new BookMarkInfo();
structBookMarkInfo = (BookMarkInfo)m_DicBookMarkInfo[pb.Name];
IEnvelope envelope = structBookMarkInfo.envelope;
IPointCollection polygon = new Polygon();
String name = pb.Name;
polygon.AddPoint(transform(envelope.XMax, envelope.YMax, name, ), transform(envelope.XMax, envelope.YMax, name, ));
polygon.AddPoint(transform(envelope.XMin, envelope.YMax, name, ), transform(envelope.XMin, envelope.YMax, name, ));
polygon.AddPoint(transform(envelope.XMax, envelope.YMin, name, ), transform(envelope.XMax, envelope.YMin, name, ));
polygon.AddPoint(transform(envelope.XMin, envelope.YMin, name, ), transform(envelope.XMin, envelope.YMin, name, ));
IGeometry geometry = polygon as IGeometry; m_ActiveView.ZoomTo(geometry);
m_ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); LayoutControlItem item = structBookMarkInfo.layoutControlItem;
item.Selected = true; foreach (String key in m_DicBookMarkInfo.Keys)
{
if (key != item.Name)
{
BookMarkInfo structBookMark = new BookMarkInfo();
structBookMark = (BookMarkInfo)m_DicBookMarkInfo[key]; LayoutControlItem otherItem = structBookMark.layoutControlItem;
otherItem.Selected = false;
}
}
} /// <summary>
/// 不同坐标系间坐标转换函数
/// </summary>
/// <param name="x">原坐标系x坐标</param>
/// <param name="y">原坐标系x坐标</param>
/// <param name="name">书签名称</param>
/// <param name="flag">判断返回x或y的对象</param>
/// <returns>转换后xy坐标</returns>
private double transform(double x, double y, String name, int flag)
{
BookMarkInfo structBookMarkInfo = new BookMarkInfo();
structBookMarkInfo = (BookMarkInfo)m_DicBookMarkInfo[name];
// 定义点
IPoint point = new PIE.Geometry.Point();
point.PutCoords(x, y);
// 设置空间参考
IGeometry geo = point as IGeometry;
geo.SpatialReference = structBookMarkInfo.spatialReference;
// 空间参考变换
geo.Transform(m_ActiveView.FocusMap.SpatialReference);
// 获取坐标值
double x1, y1;
x1 = y1 = ;
point.QueryCoords(ref x1, ref y1);
if (flag == ) return x1;
else return y1;
}
代码路径:
项目名称 | 百度云盘地址下/PIE示例程序/13.小工具集锦/地图书签/ BookMarkDemo |
数据路径 | 百度云盘地址下/PIE示例数据/栅格数据/04.World/World.tif |
视频路径 | 百度云盘地址下/PIE视频教程/13.小工具集锦/地图书签.avi |
注意:
在地图初始化的时候,图层树控件需要手动绑定地图控件:
tocControlMain.SetBuddyControl(mapControlMain as PIE.Carto.IPmdContents);
效果图: