PIE SDK地图书签

    地图书签,可以理解为暂时记录当前地图的范围和放大级别,在后续的操作中如果想回到地图之前的状态,就可以点击保存的书签就可以回到此状态,如图所示:

    地图刚加载的时候是一幅世界地图

 

   我们将地图的中心拖到南美洲,将地图放大到一定比例,创建以“南美洲”为名称的书签,如下图所示:

    创建后书签,在地图上随意拖动地图,并进行放大或缩小,然后点击“南美洲”书签,地图会定位到书签的南美洲视图范围。

下面来介绍下实现的主要代码:

要实现地图书签功能主要有两个步骤:

1、如何创建书签;

2、点击书签地图如何定位到书签的范围

一、创建书签

   显示书签的列表用的是Devexpress插件中的  LayoutControl控件,

将要保存书签的视图范围输出为图片.png格式,然后用文件流将图片转换成Image,显示在书签列表中。示例中创建的书签信息数据存储在字典表IDictionary<string,object>  m_DicBookMarkInfo中,其中值存储形式格式定义的是结构体BookMarkInfo,书签信息数据可以根据数据库的不同进行存储;

 1 /// <summary>
 2 /// 存储书签信息结构体
 3 /// </summary>
 4 public struct BookMarkInfo
 5 {
 6     public string key;//书签名称
 7     public LayoutControlItem layoutControlItem;//书签子控件
 8     public PIE.Geometry.IEnvelope envelope;//书签范围
 9     public PictureBox image;//书签范围图片
10     public PIE.Geometry.ISpatialReference spatialReference;//空间参考
11 }

 

 1 /// <summary>
 2 /// 添加地图书签
 3 /// </summary>
 4 /// <param name="sender"></param>
 5 /// <param name="e"></param>
 6 private void tbn_AddBookMark_Click(object sender, EventArgs e)
 7 {
 8     //1、获取书签的名称
 9     FrmAddBookMark bookMark = new FrmAddBookMark(m_DicBookMarkInfo);
10     if (bookMark.ShowDialog() != DialogResult.OK) return;
11     m_BookMarkName = bookMark.GetBookMarkName();
12     if (string.IsNullOrEmpty(m_BookMarkName)) return;
13 
14     //2、输出图片
15     IDisplayTransformation displayTransformation = mapControlMain.ActiveView.DisplayTransformation;
16     PIE.Carto.ExportPNG export = new ExportPNG();
17     export.Width = (int)displayTransformation.DeviceFrame.Width;
18     export.Height = (int)displayTransformation.DeviceFrame.Height;
19     export.ExportFileName = @"..\Data\Bookmark\pic\" + m_BookMarkName + ".png";
20 
21     string filePath = Path.GetDirectoryName(export.ExportFileName);
22     if (!Directory.Exists(filePath))
23     {
24         Directory.CreateDirectory(filePath);
25     }
26     export.StartExporting();
27     mapControlMain.ActiveView.Output(export as IExport, 96, displayTransformation.DeviceFrame, mapControlMain.ActiveView.Extent, null);
28     export.FinishExporting();
29 
30     //3、界面设计(将图片展示在书签列表)
31     Design(export.ExportFileName);
32 }
33 /// <summary>
34 /// 书签界面设计
35 /// </summary>
36 /// <param name="filePath">输出图片路径</param>
37 private void Design(string filePath)
38 {
39     PictureBox pictureBox1 = new PictureBox();
40     //将保存的图片展示在书签列表
41     using (FileStream image = new FileStream(filePath, FileMode.Open))
42     {
43         pictureBox1.Image = Image.FromStream(image);
44     }
45     LayoutControlItem item = layoutControlGroup1.AddItem(m_BookMarkName, pictureBox1) as LayoutControlItem;
46     item.Name = m_BookMarkName;
47 
48     pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
49     IEnvelope envelope = m_ActiveView.Extent;
50     pictureBox1.Name = m_BookMarkName;
51     pictureBox1.Size = new Size(300, 100);
52     pictureBox1.Enabled = false;
53 
54     //事件
55     item.Click += ItemPictureBox_Click;
56     item.DoubleClick += ItemPictureBox_DoubleClick;
57 
58     item.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
59     item.MaxSize = new Size(250, 150);
60     item.MinSize = new Size(250, 150);
61     item.TextLocation = DevExpress.Utils.Locations.Bottom;
62 
63     item.AppearanceItemCaption.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
64     item.Spacing = new DevExpress.XtraLayout.Utils.Padding(10);
65     item.AppearanceItemCaption.Font = new System.Drawing.Font("宋体", 13F, FontStyle.Bold);
66 
67     //存储标签信息
68     BookMarkInfo bookMarkInfo = new BookMarkInfo();
69     bookMarkInfo.key = m_BookMarkName;
70     bookMarkInfo.layoutControlItem = item;
71     bookMarkInfo.envelope = envelope;
72     bookMarkInfo.image = pictureBox1;
73     bookMarkInfo.spatialReference = mapControlMain.FocusMap.SpatialReference;
74 
75     m_DicBookMarkInfo.Add(m_BookMarkName, bookMarkInfo);//将信息存进字典表中
76 }
 

二、点击书签地图定位书签的范围

        点击书签分为单击和双击,单击时选中书签,双击时,地图范围更新变化,单击时:选中书签,其他选中的书签设置为不选中

 1 /// <summary>
 2 /// 单击事件
 3 /// </summary>
 4 /// <param name="sender"></param>
 5 /// <param name="e"></param>
 6 void ItemPictureBox_Click(object sender, EventArgs e)
 7 {
 8     LayoutControlItem item = (LayoutControlItem)sender;
 9     m_SelectedBookMarkName = item.Name;
10 
11     item.Selected = true;
12     foreach (String key in m_DicBookMarkInfo.Keys)
13     {
14         if (key != item.Name)
15         {
16             BookMarkInfo structBookMark = new BookMarkInfo();
17             structBookMark = (BookMarkInfo)m_DicBookMarkInfo[key];
18 
19            LayoutControlItem otherItem = structBookMark.layoutControlItem;
20             otherItem.Selected = false;
21         }
22     }
23 }
 

    双击时:获取存储的选中对应书签的范围,并根据地图坐标系的不同进行转换,然后将地图的视图更新为选中书签的范围

 1 /// <summary>
 2 /// 双击标签图片
 3 /// </summary>
 4 /// <param name="sender"></param>
 5 /// <param name="e"></param>
 6 void ItemPictureBox_DoubleClick(object sender, EventArgs e)
 7 {
 8     LayoutControlItem pb = (LayoutControlItem)sender;
 9     pan(pb);
10 }
11     /// <summary>
12     /// 定位函数
13     /// </summary>
14     /// <param name="pb">书签控件</param>
15     private void pan(LayoutControlItem pb)
16     {
17     BookMarkInfo structBookMarkInfo = new BookMarkInfo();
18     structBookMarkInfo = (BookMarkInfo)m_DicBookMarkInfo[pb.Name];
19     IEnvelope envelope = structBookMarkInfo.envelope;
20     IPointCollection polygon = new Polygon();
21     String name = pb.Name;
22     polygon.AddPoint(transform(envelope.XMax, envelope.YMax, name, 1), transform(envelope.XMax, envelope.YMax, name, 2));
23     polygon.AddPoint(transform(envelope.XMin, envelope.YMax, name, 1), transform(envelope.XMin, envelope.YMax, name, 2));
24     polygon.AddPoint(transform(envelope.XMax, envelope.YMin, name, 1), transform(envelope.XMax, envelope.YMin, name, 2));
25     polygon.AddPoint(transform(envelope.XMin, envelope.YMin, name, 1), transform(envelope.XMin, envelope.YMin, name, 2));
26     IGeometry geometry = polygon as IGeometry;
27 
28     m_ActiveView.ZoomTo(geometry);
29     m_ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll);
30 
31     LayoutControlItem item = structBookMarkInfo.layoutControlItem;
32     item.Selected = true;
33 
34     foreach (String key in m_DicBookMarkInfo.Keys)
35     {
36         if (key != item.Name)
37         {
38             BookMarkInfo structBookMark = new BookMarkInfo();
39             structBookMark = (BookMarkInfo)m_DicBookMarkInfo[key];
40 
41             LayoutControlItem otherItem = structBookMark.layoutControlItem;
42             otherItem.Selected = false;
43         }
44     }
45     }
46 
47     /// <summary>
48     /// 不同坐标系间坐标转换函数
49     /// </summary>
50     /// <param name="x">原坐标系x坐标</param>
51     /// <param name="y">原坐标系x坐标</param>
52     /// <param name="name">书签名称</param>
53     /// <param name="flag">判断返回x或y的对象</param>
54     /// <returns>转换后xy坐标</returns>
55     private double transform(double x, double y, String name, int flag)
56     {
57     BookMarkInfo structBookMarkInfo = new BookMarkInfo();
58     structBookMarkInfo = (BookMarkInfo)m_DicBookMarkInfo[name];
59     // 定义点
60     IPoint point = new PIE.Geometry.Point();
61     point.PutCoords(x, y);
62     // 设置空间参考
63     IGeometry geo = point as IGeometry;
64     geo.SpatialReference = structBookMarkInfo.spatialReference;
65     // 空间参考变换
66     geo.Transform(m_ActiveView.FocusMap.SpatialReference);
67     // 获取坐标值
68     double x1, y1;
69     x1 = y1 = 0;
70     point.QueryCoords(ref x1, ref y1);
71     if (flag == 1) return x1;
72     else return y1;
73     }

代码路径:

项目名称

百度云盘地址下/PIE示例程序/13.小工具集锦/地图书签/ BookMarkDemo

数据路径

百度云盘地址下/PIE示例数据/栅格数据/04.World/World.tif

视频路径

百度云盘地址下/PIE视频教程/13.小工具集锦/地图书签.avi

注意:

    在地图初始化的时候,图层树控件需要手动绑定地图控件:

    tocControlMain.SetBuddyControl(mapControlMain as PIE.Carto.IPmdContents);

    效果图: 

 

 

posted @ 2019-01-23 09:57  PIESAT  阅读(667)  评论(0编辑  收藏  举报