MapObjects2.2 在C#中的应用(基本地图功能
2024-07-21 02:19:37
供稿:网友
mapobjects2.2 在c#中的应用(基本地图功能)
关键字:mapobjects 2.2 c#
仅仅实现了地图的放大、缩小、漫游、全图、距离、面积等功能。
/*
* 杨雨田 [email protected]
* 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;
}
}
}