为MapXtreme 2004 6.1 Win 添加测距功能按钮
自从MapInfo把控件从MapX升级到MapXtreme之后,让人郁闷的是测距功能按钮在Win版本中消失了,只有Web版本中还保留着。
所以下面的方法就是为了恢复这个必备的功能:
1)为MapTool控件增加一个ToolBarButton
//
// toolBarButtonDistance
//
this.toolBarButtonDistance.ImageIndex = 11;
this.toolBarButtonDistance.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.toolBarButtonDistance.Tag = "CustomLine";
this.toolBarButtonDistance.ToolTipText = "测距";
2) 为MapControl增加工具响应事件
this.mapControl1.Tools.Add("CustomLine", new MapInfo.Tools.CustomLineMapTool(true, true, true,
mapControl1.Viewer, mapControl1.Handle.ToInt32(), mapControl1.Tools,
mapControl1.Tools.MouseToolProperties, mapControl1.Tools.MapToolProperties));
this.mapControl1.Tools.Used +=new MapInfo.Tools.ToolUsedEventHandler(Tools_Used);
private void Tools_Used(object sender, MapInfo.Tools.ToolUsedEventArgs e)
{
if(e.ToolName != "CustomLine") //不是测距事件,不处理
return ;
if (e.ToolStatus==MapInfo.Tools.ToolStatus.Start)
{
FindLonLatFromToolUsedEventArg(e.ToString(),out beginLon,out beginLat);
}
else if( e.ToolStatus==MapInfo.Tools.ToolStatus.End)
{
double endLon,endLat;
FindLonLatFromToolUsedEventArg(e.ToString(),out endLon,out endLat);
double distance=util.GetDistanceBetweenTwoLonLatPoints(beginLat,beginLon,endLat,endLon);
double distance2=Convert.ToDouble(Convert.ToInt32(distance*100))/100;
this.statusBar2.Text="两点相距: "+distance2.ToString()+" 米";
beginLon=0;beginLat=0;
endLon=0;endLat=0;
}
}
3) 辅助函数
/// <summary>
/// 根据tagname,把除了参数指定的button之外的其他button弹起
/// </summary>
/// <param name="toolName"></param>
private void CheckToolButton(string toolName)
{
foreach (ToolBarButton tbb in this.mapToolBar1.Buttons)
if (tbb.Style == ToolBarButtonStyle.ToggleButton)
tbb.Pushed = ((string)tbb.Tag == toolName);
}
/// <summary>
/// 把鼠标点击点的信息串转成经纬度
/// </summary>
/// <param name="eventArgMsg"></param>
/// <param name="lon"></param>
/// <param name="lat"></param>
private void FindLonLatFromToolUsedEventArg(string eventArgMsg,out double lon,out double lat)
{
int lonBeginIndex=eventArgMsg.IndexOf("(");
int lonEndIndex=eventArgMsg.IndexOf(", ");
int latEndIndex=eventArgMsg.IndexOf(")");
try
{
lon=Convert.ToDouble(eventArgMsg.Substring(lonBeginIndex+1,lonEndIndex-lonBeginIndex-1));
lat=Convert.ToDouble(eventArgMsg.Substring(lonEndIndex+2,latEndIndex-lonEndIndex-2));
}
catch(Exception err)
{
err.ToString();
lon=0;lat=0;
}
}
所以下面的方法就是为了恢复这个必备的功能:
1)为MapTool控件增加一个ToolBarButton
//
// toolBarButtonDistance
//
this.toolBarButtonDistance.ImageIndex = 11;
this.toolBarButtonDistance.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.toolBarButtonDistance.Tag = "CustomLine";
this.toolBarButtonDistance.ToolTipText = "测距";
2) 为MapControl增加工具响应事件
this.mapControl1.Tools.Add("CustomLine", new MapInfo.Tools.CustomLineMapTool(true, true, true,
mapControl1.Viewer, mapControl1.Handle.ToInt32(), mapControl1.Tools,
mapControl1.Tools.MouseToolProperties, mapControl1.Tools.MapToolProperties));
this.mapControl1.Tools.Used +=new MapInfo.Tools.ToolUsedEventHandler(Tools_Used);
private void Tools_Used(object sender, MapInfo.Tools.ToolUsedEventArgs e)
{
if(e.ToolName != "CustomLine") //不是测距事件,不处理
return ;
if (e.ToolStatus==MapInfo.Tools.ToolStatus.Start)
{
FindLonLatFromToolUsedEventArg(e.ToString(),out beginLon,out beginLat);
}
else if( e.ToolStatus==MapInfo.Tools.ToolStatus.End)
{
double endLon,endLat;
FindLonLatFromToolUsedEventArg(e.ToString(),out endLon,out endLat);
double distance=util.GetDistanceBetweenTwoLonLatPoints(beginLat,beginLon,endLat,endLon);
double distance2=Convert.ToDouble(Convert.ToInt32(distance*100))/100;
this.statusBar2.Text="两点相距: "+distance2.ToString()+" 米";
beginLon=0;beginLat=0;
endLon=0;endLat=0;
}
}
3) 辅助函数
/// <summary>
/// 根据tagname,把除了参数指定的button之外的其他button弹起
/// </summary>
/// <param name="toolName"></param>
private void CheckToolButton(string toolName)
{
foreach (ToolBarButton tbb in this.mapToolBar1.Buttons)
if (tbb.Style == ToolBarButtonStyle.ToggleButton)
tbb.Pushed = ((string)tbb.Tag == toolName);
}
/// <summary>
/// 把鼠标点击点的信息串转成经纬度
/// </summary>
/// <param name="eventArgMsg"></param>
/// <param name="lon"></param>
/// <param name="lat"></param>
private void FindLonLatFromToolUsedEventArg(string eventArgMsg,out double lon,out double lat)
{
int lonBeginIndex=eventArgMsg.IndexOf("(");
int lonEndIndex=eventArgMsg.IndexOf(", ");
int latEndIndex=eventArgMsg.IndexOf(")");
try
{
lon=Convert.ToDouble(eventArgMsg.Substring(lonBeginIndex+1,lonEndIndex-lonBeginIndex-1));
lat=Convert.ToDouble(eventArgMsg.Substring(lonEndIndex+2,latEndIndex-lonEndIndex-2));
}
catch(Exception err)
{
err.ToString();
lon=0;lat=0;
}
}