MapObjects2.2 在C#中的應用(基本地圖功能

來源:互聯網
上載者:User
object MapObjects2.2 在C#中的應用(基本地圖功能)

關鍵字:MapObjects 2.2 C#



僅僅實現了地圖的放大、縮小、漫遊、全圖、距離、面積等功能。



/*

* 楊雨田 blue_bat@126.com

* 2004年7月27日

*/

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace Artemis.IVS.Test

{

/// <summary>

/// 處理執行個體

/// </summary>

public class FrmMain : System.Windows.Forms.Form

{

private AxMapObjects2.AxMap mapMain;

private System.Windows.Forms.MainMenu mainMenu1;

private System.Windows.Forms.MenuItem miZoomIn;

private System.Windows.Forms.MenuItem miZoomOut;

private System.Windows.Forms.MenuItem miPan;

private System.Windows.Forms.MenuItem miFullView;

private int CurOperate = 0;

private System.Windows.Forms.MenuItem miSelectByPoint;

private System.Windows.Forms.MenuItem miDistance;

private System.Windows.Forms.MenuItem miArea;

/// <summary>

/// 必需的設計器變數。

/// </summary>

private System.ComponentModel.Container components = null;



public FrmMain()

{

//

// Windows 表單設計器支援所必需的

//

InitializeComponent();

}



/// <summary>

/// 清理所有正在使用的資源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



#region Windows 表單設計器產生的程式碼

/// <summary>

/// 設計器支援所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內容。

/// </summary>

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmMain));

this.mapMain = new AxMapObjects2.AxMap();

this.mainMenu1 = new System.Windows.Forms.MainMenu();

this.miZoomIn = new System.Windows.Forms.MenuItem();

this.miZoomOut = new System.Windows.Forms.MenuItem();

this.miPan = new System.Windows.Forms.MenuItem();

this.miFullView = new System.Windows.Forms.MenuItem();

this.miSelectByPoint = new System.Windows.Forms.MenuItem();

this.miDistance = new System.Windows.Forms.MenuItem();

this.miArea = new System.Windows.Forms.MenuItem();

((System.ComponentModel.ISupportInitialize)(this.mapMain)).BeginInit();

this.SuspendLayout();

//

// mapMain

//

this.mapMain.Dock = System.Windows.Forms.DockStyle.Fill;

this.mapMain.Location = new System.Drawing.Point(0, 0);

this.mapMain.Name = "mapMain";

this.mapMain.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mapMain.OcxState")));

this.mapMain.Size = new System.Drawing.Size(592, 401);

this.mapMain.TabIndex = 0;

this.mapMain.MouseDownEvent += new AxMapObjects2._DMapEvents_MouseDownEventHandler(this.mapMain_MouseDownEvent);

//

// mainMenu1

//

this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

this.miZoomIn,

this.miZoomOut,

this.miPan,

this.miFullView,

this.miSelectByPoint,

this.miDistance,

this.miArea});

//

// miZoomIn

//

this.miZoomIn.Index = 0;

this.miZoomIn.Text = "放大";

this.miZoomIn.Click += new System.EventHandler(this.miZoomIn_Click);

//

// miZoomOut

//

this.miZoomOut.Index = 1;

this.miZoomOut.Text = "縮小";

this.miZoomOut.Click += new System.EventHandler(this.miZoomOut_Click);

//

// miPan

//

this.miPan.Index = 2;

this.miPan.Text = "漫遊";

this.miPan.Click += new System.EventHandler(this.miPan_Click);

//

// miFullView

//

this.miFullView.Index = 3;

this.miFullView.Text = "全圖";

this.miFullView.Click += new System.EventHandler(this.miFullView_Click);

//

// miSelectByPoint

//

this.miSelectByPoint.Index = 4;

this.miSelectByPoint.Text = "點選";

this.miSelectByPoint.Click += new System.EventHandler(this.miSelectByPoint_Click);

//

// miDistance

//

this.miDistance.Index = 5;

this.miDistance.Text = "距離";

this.miDistance.Click += new System.EventHandler(this.miDistance_Click);

//

// miArea

//

this.miArea.Index = 6;

this.miArea.Text = "面積";

this.miArea.Click += new System.EventHandler(this.miArea_Click);

//

// FrmMain

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(592, 401);

this.Controls.Add(this.mapMain);

this.Menu = this.mainMenu1;

this.Name = "FrmMain";

this.Text = "Artemis.IVS.Test";

this.Load += new System.EventHandler(this.FrmMain_Load);

((System.ComponentModel.ISupportInitialize)(this.mapMain)).EndInit();

this.ResumeLayout(false);



}

#endregion



/// <summary>

/// 應用程式的主進入點。

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new FrmMain());

}



/// <summary>

/// 全圖

/// </summary>

private void miFullView_Click(object sender, System.EventArgs e)

{

this.CurOperate = 4;

mapMain.Extent = mapMain.FullExtent;

mapMain.MousePointer = MapObjects2.MousePointerConstants.moDefault;

}



/// <summary>

/// 漫遊

/// </summary>

private void miPan_Click(object sender, System.EventArgs e)

{

this.CurOperate = 3;

mapMain.MousePointer = MapObjects2.MousePointerConstants.moPan;

}



/// <summary>

/// 縮小

/// </summary>

private void miZoomOut_Click(object sender, System.EventArgs e)

{

this.CurOperate = 2;

mapMain.MousePointer = MapObjects2.MousePointerConstants.moZoomOut;

}



/// <summary>

/// 放大

/// </summary>

private void miZoomIn_Click(object sender, System.EventArgs e)

{

this.CurOperate = 1;

mapMain.MousePointer = MapObjects2.MousePointerConstants.moZoomIn;

}



/// <summary>

/// 放大縮小等的處理過程

/// </summary>

private void mapMain_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)

{

MapObjects2.Rectangle rect;

MapObjects2.Point pt = mapMain.ToMapPoint(e.x,e.y);



if(e.button == 2) this.CurOperate = 0;//右鍵點擊取消



switch(this.CurOperate)

{

case 1://放大

{

rect = mapMain.TrackRectangle();



if(rect == null|| (rect.Width < 0.00005) || (rect.Height < 0.00005))

{

rect = mapMain.Extent;

rect.ScaleRectangle(0.6667);

rect.Offset(-(rect.Center.X - pt.X),-(rect.Center.Y - pt.Y));

}

mapMain.Extent = rect;

break;

}

case 2://縮小

{

rect = mapMain.TrackRectangle();



if ((null == rect) || (rect.Width < 0.00005) || (rect.Height < 0.00005))

{

rect = mapMain.Extent;

rect.ScaleRectangle(1.5);

rect.Offset(-(rect.Center.X - pt.X),-(rect.Center.Y - pt.Y));

}

else

{

double dRate = mapMain.Extent.Width / rect.Width * 10;

rect.ScaleRectangle(dRate);

}



mapMain.Extent = rect;

break;

}

case 3://漫遊

{

mapMain.Pan();

break;

}

case 5://點選

{



break;

}

case 6://距離

{

MapObjects2.Points pts;

pts = (MapObjects2.Points)mapMain.TrackLine().Parts.Item(0);

double dDistance = 0;

for(int i=0;i<pts.Count-1;i++)

{

dDistance += Math.Sqrt(pts.Item(i).X*pts.Item(i).X + pts.Item(i).Y*pts.Item(i).Y);

}

this.Text = mapMain.ToMapDistance((float)dDistance).ToString();

break;

}

case 7://面積

{

MapObjects2.Polygon ply = mapMain.TrackPolygon();

double dArea = ply.Area;

this.Text = mapMain.ToMapDistance((float)dArea).ToString();

break;

}

default://箭頭

{

mapMain.MousePointer = MapObjects2.MousePointerConstants.moDefault;

break;

}

}

}



/// <summary>

/// 點選

/// </summary>

private void miSelectByPoint_Click(object sender, System.EventArgs e)

{

this.CurOperate = 5;

mapMain.MousePointer = MapObjects2.MousePointerConstants.moArrowQuestion;

}



/// <summary>

/// 距離

/// </summary>

private void miDistance_Click(object sender, System.EventArgs e)

{

this.CurOperate = 6;

mapMain.MousePointer = MapObjects2.MousePointerConstants.moCross;

}



/// <summary>

/// 面積

/// </summary>

private void miArea_Click(object sender, System.EventArgs e)

{

this.CurOperate = 7;

mapMain.MousePointer = MapObjects2.MousePointerConstants.moCross;

}

}

}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.