在页面上添加一个工具:
<esri:Tool ClientAction="Polyline" JavaScriptFile="" Name="RangeLine" DefaultImage="Images/MapTool/MeasureLine1.gif"
HoverImage="Images/MapTool/MeasureLine2.gif" SelectedImage="Images/MapTool/MeasureLine2.gif"
Text="测距" ToolTip="测距" ServerActionAssembly="App_Code" ServerActionClass="IdentifyRange" />
在app_code里面添加IdentifyRange类,代码如下:
/// <summary>
///IdentifyRange 的摘要说明 计算长度
/// </summary>
public class IdentifyRange : IMapServerToolAction
{
public IdentifyRange()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region IMapServerToolAction 成员
void IMapServerToolAction.ServerAction(ToolEventArgs args)
{
Map MapControl = args.Control as Map;//得到地图资源
MapPolylineEventArgs MyPolylineArg = (MapPolylineEventArgs)args;//得到地图中的线对象
System.Drawing.Point[] screen_points = MyPolylineArg.Vectors;//得到线的点集合
Double X1 = 0, Y1 = 0, X2 = 0, Y2 = 0;
Double XD = 0.0;
Double YD = 0.0;
Double AllDis = 0.0;
Double TempDis = 0.0;
for (int i = 0; i < screen_points.Length; i++)
{
if (i == 0)
{
//起始点
ESRI.ArcGIS.ADF.Web.Geometry.Point mappnt = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screen_points[i], MapControl.Extent, (Int32)MapControl.Width.Value, (Int32)MapControl.Height.Value);
X1 = mappnt.X;
Y1 = mappnt.Y;
}
else
{
//得到与起始点的距离
ESRI.ArcGIS.ADF.Web.Geometry.Point mappnt2 = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screen_points[i], MapControl.Extent, (Int32)MapControl.Width.Value, (Int32)MapControl.Height.Value);
X2 = mappnt2.X;
Y2 = mappnt2.Y;
XD = Math.Abs(X1 - X2);
YD = Math.Abs(Y1 - Y2);
// '计算2个坐标之间的距离
TempDis = Math.Sqrt(Math.Pow(XD, 2) + Math.Pow(YD, 2));
AllDis += TempDis;
}
}
AllDis = Math.Round(AllDis, 2);
string jstext = "alert('总计:" + AllDis.ToString() + "米');";
CallbackResult cr_lei = new CallbackResult(null, null, "javascript", jstext);
MapControl.CallbackResults.Add(cr_lei);
}
#endregion
}