随笔-311  评论-598  文章-0  trackbacks-0

添加相应工具

            //添加自定义测量距离
            mapControl.Tools.Add("DistanceTool"new CustomPolylineMapTool(truetruetrue, mapControl.Viewer, mapControl.Handle.ToInt32(), mapControl.Tools, mapControl.Tools.MouseToolProperties, mapControl.Tools.MapToolProperties));

            
//添加自定义测量面积
            mapControl.Tools.Add("AreaTool"new CustomPolygonMapTool(truetruetrue, mapControl.Viewer, mapControl.Handle.ToInt32(), mapControl.Tools, mapControl.Tools.MouseToolProperties, mapControl.Tools.MapToolProperties));

在工具条上添加相应的按钮,并捕捉当前按钮
        /// <summary>
        
/// 当点击工具栏上的按钮时
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void mapToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        
{
            
try {
                
string btnName = e.Button.Name;
                
switch (btnName)
                
{
                    
case "toolBarButtonDistanceTool"://当点击“测量距离”按钮时
                        mapControl.Tools.LeftButtonTool = CustomMapTools.DistanceTool;
                        
break;
                    
case "toolBarButtonAreaTool"://当点击“测量面积”按钮时
                        mapControl.Tools.LeftButtonTool = CustomMapTools.AreaTool;
                        
break;
                    
case "toolBarButtonFullScreen":
                        SetFullScreen(
true);
                        
break;
                    
case "toolBarButtonNormalScreen":
                        SetFullScreen(
false);
                        
break;
                    
case "toolBarButtonMapEye":
                        SetViewMenuItemChecked(eyeUserMapControl, 
!鹰眼栏ToolStripMenuItem.Checked);
                        
break;
                    
default:
                        
break;

                }

            }
catch(Exception ex) {
                GlobalHelper.ShowError(ex.Message);
            }

        }

当鼠标移到地图上的时候,相应功能开始工作
        /// <summary>
        
/// 当选择相应工具在地图控件上进行操作时
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void Tools_Used(object sender, MapInfo.Tools.ToolUsedEventArgs e)
        
{
            
try
            
{
                
switch (e.ToolName)
                
{
                    
//测量距离
                    case CustomMapTools.DistanceTool:
                        
switch (e.ToolStatus)
                        
{
                            
case ToolStatus.Start:
                                dblDistance 
= 0;
                                dptStart 
= e.MapCoordinate;
                                
break;
                            
case ToolStatus.InProgress:
                                dblDistance 
+= CoordSys.Distance(DistanceType.Spherical, map.Zoom.Unit, map.GetDisplayCoordSys(), dptStart, e.MapCoordinate);
                                SetStatusStripMessage(
"总长度为:" + string.Format("{0:F3}", dblDistance) + " " + CoordSys.DistanceUnitAbbreviation(map.Zoom.Unit).ToString());
                                dptStart 
= e.MapCoordinate;
                                
break;
                            
case ToolStatus.End:
                                dblDistance 
+= CoordSys.Distance(DistanceType.Spherical, map.Zoom.Unit, map.GetDisplayCoordSys(), dptStart, e.MapCoordinate);
                                SetStatusStripMessage(
"总长度为:" + string.Format("{0:F3}", dblDistance) + " " + CoordSys.DistanceUnitAbbreviation(map.Zoom.Unit).ToString());
                                map.Invalidate(
true);
                                
break;
                            
default:
                                
break;
                        }

                        
break;
                    
//测量面积
                    case CustomMapTools.AreaTool:
                        
switch (e.ToolStatus)
                        
{
                            
case ToolStatus.Start:
                                arrlstPoints.Clear();
                                dptFirstPoint 
= e.MapCoordinate;
                                arrlstPoints.Add(e.MapCoordinate);
                                
break;
                            
case ToolStatus.InProgress:
                                arrlstPoints.Add(e.MapCoordinate);
                                
break;
                            
case ToolStatus.End:
                                
//构造一个闭合环
                                arrlstPoints.Add(e.MapCoordinate);
                                
int intCount = arrlstPoints.Count;
                                
if (intCount <= 3)
                                
{
                                    SetStatusStripMessage(
"请画3个以上的点形成面来测量你所要的面积");
                                    
return;
                                }

                                DPoint[] dptPoints 
= new DPoint[intCount];
                                
for (int i = 0; i <= intCount - 1; i++)
                                
{
                                    dptPoints[i] 
= (DPoint)arrlstPoints[i];

                                }

                                dptPoints[intCount 
- 1= dptFirstPoint;

                                
//用闭合的环构造一个面        
                                AreaUnit costAreaUnit;
                                costAreaUnit 
= CoordSys.GetAreaUnitCounterpart(map.Zoom.Unit);
                                CoordSys objCoordSys 
= this.map.GetDisplayCoordSys();
                                Polygon objPolygon 
= new Polygon(objCoordSys, CurveSegmentType.Linear, dptPoints);
                                
//if (objPolygon == null)
                                
//    return;
                                SetStatusStripMessage("总面积为:" + string.Format("{0:F3}", objPolygon.Area(costAreaUnit)) + "  " + CoordSys.AreaUnitAbbreviation(costAreaUnit));
                                map.Invalidate(
true);
                                
break;
                            
default:
                                
break;
                        }

                        
break;
                    
//可以添加其他的用户自定义Tool
                    default:
                        
break;
                }

            }

            
catch (Exception Ex)
            
{
                GlobalHelper.ShowError(
"测量错误:" + Ex.Message.ToString());
            }
            
        }


---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2008-02-25 14:30 TiGERTiAN 阅读(2488) 评论(3)  编辑  收藏 所属分类: DotNetMapXtreme

评论:
# re: Mapxtreme 添加自定义工具,测量距离,测量面积等等 2010-02-22 13:27 | eee
您好,请问CustomMapTools是命名空间吗?需要引用那个呢  回复  更多评论
  
# re: Mapxtreme 添加自定义工具,测量距离,测量面积等等 2010-02-22 14:12 | TiGERTiAN
@eee
是一个类或枚举的名称  回复  更多评论
  
# re: Mapxtreme 添加自定义工具,测量距离,测量面积等等 2011-01-26 10:52 | 东方未明
好文章,帮大忙了,顶一下!
此外,CustomMapTools我也不太清楚楼主是什么意思,好像MapXtream中没有此命名空间或类,我采用下面的调用方法也可以正常工作:
mapControl.Tools.LeftButtonTool = "DistanceTool"; // MapControl mapControl  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: