MapX 控件在C# 中的应用 (一)
关键字:MapX,C#,Net,ActiveX控件
/*
* 在这段代码中,我们应用MapX控件结合老美地图做了最基本的
* 地图浏览功能:放大、缩小、拖动、全图、测量距离、测量面积。
*
* 杨雨田 YangYutian@Hotmail.Com
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace wa
{
public class frmMain : System.Windows.Forms.Form
{
private AxMapXLib.AxMap mapMain;
private double MapZoom;
private double CenterX;
private double CenterY;
private System.Windows.Forms.StatusBar sbMain;
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.MenuItem miMap;
private System.Windows.Forms.MenuItem miMapToolZoomIn;
private System.Windows.Forms.MenuItem miMapToolZoomout;
private System.Windows.Forms.MenuItem miMapToolPan;
private System.Windows.Forms.MenuItem miMapToolRestore;
private System.Windows.Forms.MenuItem miMapToolDistance;
private System.Windows.Forms.MenuItem miMapToolArea;
private System.Windows.Forms.MenuItem miMapTool;
private System.ComponentModel.Container components = null;
public frmMain()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
//略
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
//略
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void mapMain_PolyToolUsed(object sender, AxMapXLib.CMapXEvents_PolyToolUsedEvent e)
{
if(e.toolNum == 99)//测量距离
{
MapXLib.Points pts = (MapXLib.Points)e.points;
MapXLib.Point pt1,pt2;
double d=0.0;
//计算顺序两个点距离,累计得到总距离
for(int i=1;i<pts.Count;i++)
{
pt1=pts.Item(i);
pt2=pts.Item(i+1);
d += mapMain.Distance(pt1.X,pt1.Y,pt2.X,pt2.Y);
}
sbMain.Text = "距离:" + d.ToString();
}
else if(e.toolNum == 98)//面积
{
MapXLib.Points pts = (MapXLib.Points)e.points;
//偷懒了但是很正确
MapXLib.FeatureFactory dd = mapMain.FeatureFactory;
MapXLib.Style style = mapMain.DefaultStyle;
sbMain.Text = "面积:" + dd.CreateRegion(pts,style).Area.ToString();
}
}
private void miMapToolZoomIn_Click(object sender, System.EventArgs e)
{
//放大
mapMain.CurrentTool = MapXLib.ToolConstants.miZoomInTool;
}
private void miMapToolZoomout_Click(object sender, System.EventArgs e)
{
//缩小
mapMain.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
}
private void miMapToolPan_Click(object sender, System.EventArgs e)
{
//拖动
mapMain.CurrentTool = MapXLib.ToolConstants.miPanTool;
}
private void miMapToolRestore_Click(object sender, System.EventArgs e)
{
//缩放到初始大小(全图)
mapMain.ZoomTo(this.MapZoom,this.CenterX,this.CenterY);
}
private void miMapToolDistance_Click(object sender, System.EventArgs e)
{
//测量距离
mapMain.CurrentTool = (MapXLib.ToolConstants)99;
}
private void miMapToolArea_Click(object sender, System.EventArgs e)
{
//测量面积
mapMain.CurrentTool = (MapXLib.ToolConstants) 98;
}
private void frmMain_Load(object sender, System.EventArgs e)
{
//记录地图的比例合中心点
this.MapZoom = mapMain.Zoom;
this.CenterX = mapMain.CenterX;
this.CenterY = mapMain.CenterY;
//创建测量距离的工具
mapMain.CreateCustomTool(99,(short)MapXLib.ToolTypeConstants.miToolTypePoly,MapXLib.CursorConstants.miCrossCursor,MapXLib.CursorConstants.miCrossCursor,MapXLib.CursorConstants.miCrossCursor,false);
//创建测量面积的工具
mapMain.CreateCustomTool(98,(short)MapXLib.ToolTypeConstants.miToolTypePolygon,MapXLib.CursorConstants.miCrossCursor,MapXLib.CursorConstants.miCrossCursor,MapXLib.CursorConstants.miCrossCursor,false);
}
}
}