昨天晚上在家没事,用MapObjects 2.2在C#中实现地图的图层的加载、放大、缩小、平移、全图功能,把代码拿出来和大家分享一下,代码写的不好的地方,请大家多多指教。
界面如下:

代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MapObJect
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBarButton Open;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton ZoomIn;
private System.Windows.Forms.ToolBarButton ZoomOut;
private System.Windows.Forms.ToolBarButton Pan;
private System.Windows.Forms.ToolBarButton toolBarButton6;
private System.Windows.Forms.ToolBarButton ViewOver;
private AxMapObjects2.AxMap axMap1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.ComponentModel.IContainer components;
private int toolBarButtonValue;


public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

Windows 窗体设计器生成的代码

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button.Tag.ToString()=="Open")
{
ShowOpenFileDialog();
}
if(e.Button.Tag.ToString()=="ZoomIn")
{
toolBarButtonValue=1;
}

if(e.Button.Tag.ToString()=="ZoomOut")
{
toolBarButtonValue=2;
}

if(e.Button.Tag.ToString()=="Pan")
{
toolBarButtonValue=3;
}

if(e.Button.Tag.ToString()=="ViewOver")
{
axMap1.Extent=axMap1.FullExtent;
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
openFileDialog1.Filter="Sharp File|*.shp|All File|*.*";
openFileDialog1.InitialDirectory="F:\Source\MapObject\Data\USA";

axMap1.MouseDownEvent+=new AxMapObjects2._DMapEvents_MouseDownEventHandler(axMap1_MouseDownEvent);
axMap1.MouseMoveEvent+=new AxMapObjects2._DMapEvents_MouseMoveEventHandler(axMap1_MouseMoveEvent);
}

private void ShowOpenFileDialog()
{
string strFilePath,strFileName,strSpliter1,strSpliter2;
string[] strSplit1,strSplit2;
int intCount;

strSpliter1=@"";
strSpliter2=@".";
strFilePath="";
strFileName="";
openFileDialog1.FileName="";
openFileDialog1.ShowDialog();
strSplit1=openFileDialog1.FileName.Split(strSpliter1.ToCharArray());
intCount=strSplit1.Length;
for(int i=0;i<intCount-1;i++)
{
strFilePath=strFilePath+strSplit1[i]+@"";
}
for(int i=intCount-1;i<intCount;i++)
{
strSplit2=strSplit1[i].Split(strSpliter2.ToCharArray());
strFileName=strSplit2[0];
}
AddLayer(strFilePath,strFileName);
}

private void AddLayer(string strDatabase,string strDatasetName)
{
MapObjects2.DataConnection objDataConn;
MapObjects2.GeoDataset objGeoDataset;
MapObjects2.MapLayer objMapLayer;

objDataConn=new MapObjects2.DataConnectionClass();
objMapLayer=new MapObjects2.MapLayerClass();

objDataConn.Database=strDatabase;
if(objDataConn.Connect())
{
objGeoDataset=objDataConn.FindGeoDataset(strDatasetName);
objMapLayer.GeoDataset=objGeoDataset;
objMapLayer.Symbol.Color=Convert.ToUInt32( MapObjects2.ColorConstants.moYellow);

axMap1.Layers.Add(objMapLayer);
axMap1.Refresh();
}
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
ShowOpenFileDialog();
}

private void menuItem3_Click(object sender, System.EventArgs e)
{
axMap1.Layers.Clear();
axMap1.Refresh();
}

private void axMap1_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)
{
MapObjects2.Rectangle objRectangle;

if(toolBarButtonValue==1)
{
objRectangle=axMap1.TrackRectangle();
axMap1.Extent=objRectangle;
}

if(toolBarButtonValue==2)
{
objRectangle=axMap1.Extent;
objRectangle.ScaleRectangle(1.5);
axMap1.Extent=objRectangle;
}

if(toolBarButtonValue==3)
{
axMap1.Pan();
}
}

private void axMap1_MouseMoveEvent(object sender,AxMapObjects2._DMapEvents_MouseMoveEvent e)
{
if(toolBarButtonValue==1)
{
axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomIn;
}

if(toolBarButtonValue==2)
{
axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomOut;
}

if(toolBarButtonValue==3)
{
axMap1.MousePointer=MapObjects2.MousePointerConstants.moPan;
}
}
}
}
界面如下:

代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MapObJect
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBarButton Open;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton ZoomIn;
private System.Windows.Forms.ToolBarButton ZoomOut;
private System.Windows.Forms.ToolBarButton Pan;
private System.Windows.Forms.ToolBarButton toolBarButton6;
private System.Windows.Forms.ToolBarButton ViewOver;
private AxMapObjects2.AxMap axMap1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.ComponentModel.IContainer components;
private int toolBarButtonValue;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button.Tag.ToString()=="Open")
{
ShowOpenFileDialog();
}
if(e.Button.Tag.ToString()=="ZoomIn")
{
toolBarButtonValue=1;
}
if(e.Button.Tag.ToString()=="ZoomOut")
{
toolBarButtonValue=2;
}
if(e.Button.Tag.ToString()=="Pan")
{
toolBarButtonValue=3;
}
if(e.Button.Tag.ToString()=="ViewOver")
{
axMap1.Extent=axMap1.FullExtent;
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
openFileDialog1.Filter="Sharp File|*.shp|All File|*.*";
openFileDialog1.InitialDirectory="F:\Source\MapObject\Data\USA";
axMap1.MouseDownEvent+=new AxMapObjects2._DMapEvents_MouseDownEventHandler(axMap1_MouseDownEvent);
axMap1.MouseMoveEvent+=new AxMapObjects2._DMapEvents_MouseMoveEventHandler(axMap1_MouseMoveEvent);
}
private void ShowOpenFileDialog()
{
string strFilePath,strFileName,strSpliter1,strSpliter2;
string[] strSplit1,strSplit2;
int intCount;
strSpliter1=@"";
strSpliter2=@".";
strFilePath="";
strFileName="";
openFileDialog1.FileName="";
openFileDialog1.ShowDialog();
strSplit1=openFileDialog1.FileName.Split(strSpliter1.ToCharArray());
intCount=strSplit1.Length;
for(int i=0;i<intCount-1;i++)
{
strFilePath=strFilePath+strSplit1[i]+@"";
}
for(int i=intCount-1;i<intCount;i++)
{
strSplit2=strSplit1[i].Split(strSpliter2.ToCharArray());
strFileName=strSplit2[0];
}
AddLayer(strFilePath,strFileName);
}
private void AddLayer(string strDatabase,string strDatasetName)
{
MapObjects2.DataConnection objDataConn;
MapObjects2.GeoDataset objGeoDataset;
MapObjects2.MapLayer objMapLayer;
objDataConn=new MapObjects2.DataConnectionClass();
objMapLayer=new MapObjects2.MapLayerClass();
objDataConn.Database=strDatabase;
if(objDataConn.Connect())
{
objGeoDataset=objDataConn.FindGeoDataset(strDatasetName);
objMapLayer.GeoDataset=objGeoDataset;
objMapLayer.Symbol.Color=Convert.ToUInt32( MapObjects2.ColorConstants.moYellow);
axMap1.Layers.Add(objMapLayer);
axMap1.Refresh();
}
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
ShowOpenFileDialog();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
axMap1.Layers.Clear();
axMap1.Refresh();
}
private void axMap1_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)
{
MapObjects2.Rectangle objRectangle;
if(toolBarButtonValue==1)
{
objRectangle=axMap1.TrackRectangle();
axMap1.Extent=objRectangle;
}
if(toolBarButtonValue==2)
{
objRectangle=axMap1.Extent;
objRectangle.ScaleRectangle(1.5);
axMap1.Extent=objRectangle;
}
if(toolBarButtonValue==3)
{
axMap1.Pan();
}
}
private void axMap1_MouseMoveEvent(object sender,AxMapObjects2._DMapEvents_MouseMoveEvent e)
{
if(toolBarButtonValue==1)
{
axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomIn;
}
if(toolBarButtonValue==2)
{
axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomOut;
}
if(toolBarButtonValue==3)
{
axMap1.MousePointer=MapObjects2.MousePointerConstants.moPan;
}
}
}
}

浙公网安备 33010602011771号