利用工具方式进行地图操作,包括两个事件,第一个事件是工具栏上按钮点击事件;第二个事件是MapObjects控件上的操作事件(MouseDown事件)。
对第一个事件的响应通常是改变鼠标指针的形状,同时记录按钮的功能。
-
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
-
{
-
switch (e.ClickedItem.Name)
-
{
-
case "Zoomin":
-
map_mouse_symbol = "zoomin";
-
axMap1.MousePointer = MapObjects2.MousePointerConstants.moZoomIn;
-
break;
-
case "Zoomout":
-
map_mouse_symbol = "zoomout";
-
axMap1.MousePointer = MapObjects2.MousePointerConstants.moZoomOut;
-
break;
-
case "Pan":
-
map_mouse_symbol = "pan";
-
axMap1.MousePointer = MapObjects2.MousePointerConstants.moPan;
-
break;
-
case "FullView":
-
map_mouse_symbol = "fullview";
-
axMap1.MousePointer = MapObjects2.MousePointerConstants.moArrow;
-
axMap1.Extent = axMap1.FullExtent;
-
axMap1.Refresh();
-
break;
-
}
-
}
对于第二个事件的响应是根据点击按钮的功能,从而执行相应的代码。如果是放大事件,则利用MapObjects控件的TrackRectangle返回用户在地图上定义的Rectangle,并返回放大后地图的范围;如果是缩小事件,则根据定义的Rectangle大小确定比例,即地图控件高度(或宽度)与用户定义的Rectangle高度(或宽度)之比,Rectangle越小,缩小的比例越大;如果是漫游功能,则利用MapObjects控件的Pan方法。
-
MapObjects2.Rectangle rect; //范围
-
switch (axMap1.MousePointer)
-
{
-
//放大
-
case MapObjects2.MousePointerConstants.moZoomIn:
-
{
-
rect = axMap1.TrackRectangle(); //得到鼠标拖动范围
-
if (rect == null || rect.Width < 0.00005 || rect.Height < 0.00005) //如果选择区域很小,可以认为是 单击、点选
-
{
-
rect = axMap1.Extent;
-
rect.ScaleRectangle(0.5); //设置放大比例
-
}
-
axMap1.Extent = rect;
-
break;
-
}
-
-
//缩小 ,当点击图层缩小时,由于宽度和高度为0,将会发生分母为0错误,用户可自行解决
-
case MapObjects2.MousePointerConstants.moZoomOut:
-
{
-
MapObjects2.Rectangle Tempr;
-
Tempr = axMap1.Extent;
-
rect = axMap1.TrackRectangle();
-
double NewSR;
-
if (rect == null || rect.Width < 0.00005 || rect.Height < 0.00005)
-
{
-
if (axMap1.Extent.Width / rect.Width > axMap1.Extent.Height / rect.Height)
-
{
-
NewSR = axMap1.Extent.Height / rect.Height;
-
}
-
else
-
{
-
NewSR = axMap1.Extent.Width / rect.Width;
-
}
-
Tempr.ScaleRectangle(NewSR);
-
}
-
else
-
{
-
Tempr.ScaleRectangle(2.0); //设置缩小比率
-
}
-
axMap1.Extent = Tempr;
-
break;
-
}
-
//漫游
-
case MapObjects2.MousePointerConstants.moPan:
-
{
-
axMap1.Pan();
-
break;
-
}
阅读(2364) | 评论(0) | 转发(0) |