Mapxtreme 添加自定义工具,测量距离,测量面积等等
添加相应工具
//添加自定义测量距离
mapControl.Tools.Add("DistanceTool", new CustomPolylineMapTool(true, true, true, mapControl.Viewer, mapControl.Handle.ToInt32(), mapControl.Tools, mapControl.Tools.MouseToolProperties, mapControl.Tools.MapToolProperties));
//添加自定义测量面积
mapControl.Tools.Add("AreaTool", new CustomPolygonMapTool(true, true, true, mapControl.Viewer, mapControl.Handle.ToInt32(), mapControl.Tools, mapControl.Tools.MouseToolProperties, mapControl.Tools.MapToolProperties));
mapControl.Tools.Add("DistanceTool", new CustomPolylineMapTool(true, true, true, mapControl.Viewer, mapControl.Handle.ToInt32(), mapControl.Tools, mapControl.Tools.MouseToolProperties, mapControl.Tools.MapToolProperties));
//添加自定义测量面积
mapControl.Tools.Add("AreaTool", new CustomPolygonMapTool(true, true, true, 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>
/// <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());
}
}
/// 当选择相应工具在地图控件上进行操作时
/// </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());
}
}