自從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", newMapInfo.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(),outbeginLon,out beginLat);
}
else if( e.ToolStatus==MapInfo.Tools.ToolStatus.End)
{
double endLon,endLat;
FindLonLatFromToolUsedEventArg(e.ToString(),outendLon,out endLat);
doubledistance=util.GetDistanceBetweenTwoLonLatPoints(beginLat,beginLon,endLat,endLon);
doubledistance2=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 voidFindLonLatFromToolUsedEventArg(string eventArgMsg,out double lon,outdouble 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;
}
}