最近在学mo,主要用c#,由于这方面的资料少,自己的e文又不好,所以过了很长时间还是收效甚微,真是郁闷。
这是我的一个小程序,主要用mo+c#,实现了基本的图层动态添加,放大、缩小、漫游等基本功能。
以下是代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using DevComponents.DotNetBar;
namespace gisLx{ public partial class frmMain : Form { public frmMain() { InitializeComponent(); }
private void frmMain_Load(object sender, EventArgs e) { //===============加入地图图层==================// //定义数据连接 ESRI.MapObjects2.Core.DataConnection moCon = new ESRI.MapObjects2.Core.DataConnectionClass(); //定义图层 ESRI.MapObjects2.Core.MapLayer moLay1 = new ESRI.MapObjects2.Core.MapLayer(); ESRI.MapObjects2.Core.MapLayer moLay2 = new ESRI.MapObjects2.Core.MapLayer(); ESRI.MapObjects2.Core.MapLayer moLay3 = new ESRI.MapObjects2.Core.MapLayer(); ESRI.MapObjects2.Core.MapLayer moLay4 = new ESRI.MapObjects2.Core.MapLayer(); //地图文件都存在用用程序的Data目录下 string shpPath = Application.StartupPath + "file://Data//"; moCon.Database = shpPath;
if (!moCon.Connect()) //连接不成功就退出 { MessageBox.Show("在指定的文件夹下没找到图层数据文件"); Application.Exit(); } else //连接成功了就加载地图,地图加载顺序一般是面、线、点,先加载的在地图的最下.云南州界 { moLay1.GeoDataset = moCon.FindGeoDataset("bou1_4l"); this.axMap1.Layers.Add(moLay1);
moLay2.GeoDataset = moCon.FindGeoDataset("bou2_4l"); this.axMap1.Layers.Add(moLay2);
moLay3.GeoDataset = moCon.FindGeoDataset("roa_4m"); this.axMap1.Layers.Add(moLay3);
moLay4.GeoDataset = moCon.FindGeoDataset("XianCh_point"); this.axMap1.Layers.Add(moLay4); } //必须进行刷新后才能显示 this.axMap1.Refresh(); }
private void toolStripButton1_Click(object sender, EventArgs e) { //=============鼠标指针变为放大图标================// this.axMap1.MousePointer = ESRI.MapObjects2.Core.MousePointerConstants.moZoomIn; }
private void toolStripButton2_Click(object sender, EventArgs e) { //=============鼠标指针变为缩小图标================// this.axMap1.MousePointer = ESRI.MapObjects2.Core.MousePointerConstants.moZoomOut; }
private void toolStripButton3_Click(object sender, EventArgs e) { //=============鼠标指针变为漫游图标================// this.axMap1.MousePointer = ESRI.MapObjects2.Core.MousePointerConstants.moPan; }
private void axMap1_MouseDownEvent(object sender, ESRI.MapObjects2.Core.MouseDownEventArgs e) { //================鼠标在地图控件按下事件================// switch (this.axMap1.MousePointer) { case ESRI.MapObjects2.Core.MousePointerConstants.moZoomIn: { //放大 ESRI.MapObjects2.Core.Rectangle rect; rect = this.axMap1.TrackRectangle();
double dWith = rect.Width; double dHeight = rect.Height;
if ((rect == null) || (dWith < 0.00005) || (dHeight < 0.00005)) { ESRI.MapObjects2.Core.Point pt = this.axMap1.ToMapPoint(e.x,e.y); ESRI.MapObjects2.Core.Rectangle r = this.axMap1.Extent; r.ScaleRectangle(0.6667); r.Offset(-(r.Center.X-pt.X),-(r.Center.Y-pt.Y)); this.axMap1.Extent = r; } else { this.axMap1.Extent = rect; } break; } case ESRI.MapObjects2.Core.MousePointerConstants.moZoomOut: { //缩小 ESRI.MapObjects2.Core.Rectangle rect;
rect = this.axMap1.TrackRectangle(); if ((rect == null) || (rect.Width < 0.00005) || (rect.Height < 0.00005)) { rect = this.axMap1.Extent; rect.ScaleRectangle(1.5); } else { double dRout = this.axMap1.Extent.Width / rect.Width * 10; rect.ScaleRectangle(dRout); } this.axMap1.Extent = rect; break; } case ESRI.MapObjects2.Core.MousePointerConstants.moPan: { //漫游 this.axMap1.Pan(); break; } }
}
private void toolStripButton4_Click(object sender, EventArgs e) { //充满 this.axMap1.Extent = this.axMap1.FullExtent; }
private void dotNetBarManager1_ContainerLoadControl(object sender, EventArgs e) { //================加载自定义控件================// BaseItem item = sender as BaseItem; if (item == null) return; if (item.Name == "DockMapIndex") { DockContainerItem dockItem = item as DockContainerItem; treeMapIndex mapIndex = new treeMapIndex(); dockItem.Control = mapIndex; } else if (item.Name == "DockLayerControl") { DockContainerItem dockItem = item as DockContainerItem; dockItem.Control = new treeMapIndex(); } else if (item.Name == "DockLayerTuli") { DockContainerItem dockItem = item as DockContainerItem; dockItem.Control = new treeMapIndex(); } else if (item.Name == "DockFind") { DockContainerItem dockItem = item as DockContainerItem; dockItem.Control = new treeMapIndex(); } }
private void toolStripButton5_Click(object sender, EventArgs e) { ESRI.MapObjects2.Core.MapLayer lyr = (ESRI.MapObjects2.Core.MapLayer)axMap1.Layers.Item(0); lyr.Visible = false;
}
private void axMap1_AfterLayerDraw(object sender, ESRI.MapObjects2.Core.AfterLayerDrawEventArgs e) { 定义区域 //ESRI.MapObjects2.Core.Rectangle FullExtent; //ESRI.MapObjects2.Core.Rectangle extent; 得到整个地图的范围 //FullExtent = this.axMap1.FullExtent; 的到当前地图的显示范围 //extent = this.axMap1.Extent; //bool showDetail = (extent.Width < (FullExtent.Width / 4)) ? true : false; 得到第一层,可以指定图层 //ESRI.MapObjects2.Core.IMoLayers lyr; //lyr = this.axMap1.Layers.Item(0); //if (showDetail == true) //{ lyr.Item.Visible = true; } //else //{ lyr.Item.Visible = false; }
} }}