ArcGIS Engine开发之书签加载
ArcGIS中书签是保存特定视图范围的快捷方式。使用书签保存关注的视图范围,可在需要时快速定位、查看与浏览。书签功能主要用到IMapBookmarks、ISpatialBookmark和IAOIBookmark三个接口。
通过IMapBookmarks接口的Bookmarks属性可以得到地图文档中已经存在的所有书签对象,通过该接口的AddBookmark和RemoveBookmark方法可以对书签进行添加和删除。
ISpatialBookmark接口定义所有空间书签的共同功能,特别是书签的Name属性和ZoomTo方法。Name属性定义了书签的名字,通过ZoomTo方法可跳转到书签位置点。
IAOIBookmark接口继承自ISpatialBookmark接口,新增了Location属性,用于存储地图中感兴趣的范围。
1、添加书签
新建一个窗体用于设置书签名称,窗体界面如图:
实现:
public partial class FormBookMarks : DevExpress.XtraEditors.XtraForm { /// <summary> /// 定义全局变量 /// </summary> /// private string m_bookmark;//书签名 private int m_check;//是否创建书签 public FormBookMarks() { InitializeComponent(); } private void FormBookMarks_Load(object sender, EventArgs e) { //设置确定按钮一开始的不可用性 btnOk.Enabled = false; } /// <summary> /// 确定按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOk_Click(object sender, EventArgs e) { m_bookmark = txtBookMarks.Text; txtBookMarks.Text = ""; m_check = 1; this.Close(); } //取消按钮 private void btnCel_Click(object sender, EventArgs e) { txtBookMarks.Text = ""; m_check = 0; this.Close(); } //设置书签为只读 public string Bookmark { get { return m_bookmark; } } //是否创建书签变量为只读 public int Check { get { return m_check; } } /// <summary> /// 在TextBox中文本改变事件中设置按钮的可用性 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtBookMarks_TextChanged(object sender, EventArgs e) { if (txtBookMarks.Text == "") { btnOk.Enabled = false; } else { btnOk.Enabled = true; } }
mainForm中的代码
/// <summary> /// 添加书签的操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpenBookMark_ItemClick(object sender, ItemClickEventArgs e) { FormBookMarks frmBookMark = new FormBookMarks(); frmBookMark.ShowDialog(); string pName = string.Empty; int check = frmBookMark.Check; if (check == 1) { pName = frmBookMark.Bookmark; } if (string.IsNullOrEmpty(pName)) return; //书签进行重名判断 IMapBookmarks mapBookmarks= mainMapControl.Map as IMapBookmarks; IEnumSpatialBookmark enumSpatialBookmarks = mapBookmarks.Bookmarks; enumSpatialBookmarks.Reset(); ISpatialBookmark pSpatialBookmark; while ((pSpatialBookmark = enumSpatialBookmarks.Next()) != null) { if (pName == pSpatialBookmark.Name) { DialogResult dr = MessageBox.Show("此书签名已存在!是否替换?", "提示", MessageBoxButtons.YesNoCancel); if (dr == DialogResult.Yes) { mapBookmarks.RemoveBookmark(pSpatialBookmark); } else if (dr == DialogResult.No) { frmBookMark.ShowDialog(); } else { return; } } } //获取当前地图的对象 IActiveView pActiveView = mainMapControl.Map as IActiveView; //创建一个新的书签并设置其位置范围为当前视图的范围 IAOIBookmark pBookmark = new AOIBookmarkClass(); pBookmark.Location = pActiveView.Extent; //获得书签名 pBookmark.Name = pName; //通过IMapBookmarks接口访问当前地图书签集,添加书签到地图的书签集中 IMapBookmarks pMapBookmarks = mainMapControl.Map as IMapBookmarks; pMapBookmarks.AddBookmark(pBookmark); } /// <summary> /// 书签管理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpenMangerBookMark_ItemClick(object sender, ItemClickEventArgs e) { try { FormMangerBookMark frmManageBookmark = new FormMangerBookMark(mainMapControl.Map); frmManageBookmark.ShowDialog(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
新建书签管理窗体,如图:
代码:
public partial class FormMangerBookMark : DevExpress.XtraEditors.XtraForm { /// <summary> /// 定义全局变量 /// </summary> /// private IMap _currentMap = null; Dictionary<string, ISpatialBookmark> pDictionary = new Dictionary<string, ISpatialBookmark>(); IMapBookmarks mapBookmarks = null; public FormMangerBookMark(IMap pMap) { InitializeComponent(); _currentMap = pMap;//获取当前地图 InitControl(); } /// <summary> /// 获取空间书签,对ListBookmark进行初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// private void InitControl() { mapBookmarks = _currentMap as IMapBookmarks; IEnumSpatialBookmark enumSpatialBookmarks = mapBookmarks.Bookmarks; enumSpatialBookmarks.Reset(); ISpatialBookmark pSpatialBookmark = enumSpatialBookmarks.Next(); string sBookMarkName = string.Empty; while (pSpatialBookmark != null) { sBookMarkName = pSpatialBookmark.Name; //增加树节点 treeView1.Nodes.Add(sBookMarkName ); // advTree1 . Nodes.Add(sBookMarkName); //添加到数据字典 pDictionary.Add(sBookMarkName, pSpatialBookmark); pSpatialBookmark = enumSpatialBookmarks.Next(); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormMangerBookMark_Load(object sender, EventArgs e) { } /// <summary> /// 定位按钮的功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPosition_Click(object sender, EventArgs e) { //DevComponents.AdvTree.Node pSelectedNode =advTree1.SelectedNode; TreeNode pSelectedNode = treeView1.SelectedNode; //获取书签中的书签范围 ISpatialBookmark pSpatialBM = pDictionary[pSelectedNode.Text]; //缩放到选中的范围 pSpatialBM.ZoomTo(_currentMap); IActiveView pActiveView = _currentMap as IActiveView; pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } /// <summary> /// 删除书签的操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDeleterBookMark_Click(object sender, EventArgs e) { // DevComponents.AdvTree.Node pSelectedNode = advTree1.SelectedNode; TreeNode pSelectedNode = treeView1.SelectedNode; ISpatialBookmark pSpatialBM=pDictionary [pSelectedNode .Text ]; //删除选中的书签对象 mapBookmarks.RemoveBookmark(pSpatialBM); //删除字典中数据 pDictionary.Remove(pSelectedNode.Text ); //删除树节点 treeView1.Nodes.Remove(pSelectedNode ); treeView1.Refresh(); } /// <summary> /// 取消按钮的设置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCloseBookMark_Click(object sender, EventArgs e) { Close(); } /// <summary> /// 鼠标双击定位设置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { btnPosition.PerformClick(); }
转载时请注明出处!