// sunny code 6 Nov 2004 // 自定义测距工具 using System; using MapInfo.Mapping; using MapInfo.Web.UI.WebControls; using MapInfo.Geometry; namespace WebGIS { /// /// Summary description for customAreaTool. /// public class customAreaTool:MapTool { private string _clientStartMethod; private string _clientStopMethod; private string _clientCodeSource = null; private string _name; private double dblArea =0; public static readonly string Toolname = "customAreaTool"; public delegate void customAreaToolEventHander( Object sender, customAreaToolEnventArgs e ); public event customAreaToolEventHander CalculateFinished; public customAreaTool() { // // TODO: Add constructor logic here // _name = Toolname; // We use MapInfo's rectangle drawing client side code ClientStartMethod = "MapInfoWebPolygonStart"; ClientStopMethod = "MapInfoWebPolygonStop"; } protected void Reset() { CalculateFinished = null; } /// /// Name of the tool, by default it is ZoomInTool /// public override string Name { get { return _name; } set { _name = value; } } /// /// Name of the client side method to set up mouse event handlers for drawing /// public override string ClientStartMethod { get { return _clientStartMethod; } set { _clientStartMethod = value; } } /// /// Name of the client side method to cancel the mouse event handlers /// public override string ClientStopMethod { get { return _clientStopMethod; } set { _clientStopMethod = value; } } /// /// Source or url where the client drawing code is located /// public override string ClientCodeSource { get { return _clientCodeSource; } set { _clientCodeSource = value; } } /// /// Url of the cursor image when this tool is active /// public override string CursorUrl { get {return ""; } set {; } } public double Area() { return dblArea; } /// /// 计算客户端传回区域的面积 /// public override void Execute(string dataString, System.Collections.ArrayList arrayList, MapInfo.Mapping.Map map) { // Extract points from the string System.Drawing.Point [] points = base.ExtractPoints(dataString); //如果点数少于2,就返回 if( points.Length <= 2 ) return; MapInfo.Geometry.DPoint [] temppnts = null; //获得前端矩形操作的坐标序列 map.DisplayTransform.FromDisplay(points,out temppnts); int len = temppnts.Length; MapInfo.Geometry.DPoint [] pnts = new DPoint[len+1]; for(int i=0;i< len ;i++) { pnts[i] = temppnts[i]; } pnts[len] = temppnts[0]; //在后台生成一个相对应的多边形 MapInfo.Geometry.CoordSys coordsys = null; coordsys = map.GetDisplayCoordSys(); MapInfo.Geometry.Polygon poly = new Polygon(coordsys,MapInfo.Geometry.CurveSegmentType.Linear,pnts); dblArea = poly.Area(MapInfo.Geometry.AreaUnit.SquareKilometer); //执行完测量操作,触发该事件 if(CalculateFinished != null) { customAreaToolEnventArgs e = new customAreaToolEnventArgs(map, dblArea ); CalculateFinished(this,e); } } }///~:end of class customAreaTool //此类,用于传递事件参数 public class customAreaToolEnventArgs : EventArgs { private Map _map; private double _area; public customAreaToolEnventArgs( Map map, double area) { this._map = map; this._area = area; } public double Area { get { return _area; } } }///~:end of class customAreaToolEnventArgs // 此类,将自定义工具自定义为服务器端控件。 public class customAreaToolControl : ToolControl { public event customAreaTool.customAreaToolEventHander CalculateFinished; public customAreaToolControl() { TooltipText = "测量面积"; crsorImageUrl = "/MapXtremeWebResources/MapInfoWebPolygonSelection.cur"; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if( this.mapControl != null ) { customAreaTool MyAreaTool = new customAreaTool(); this.MapTool = MyAreaTool; this.mapControl.MapTools.Add( MyAreaTool ); MyAreaTool.CalculateFinished += CalculateFinished; } } }///~: end of class customAreaToolControl } //end of namespace WebGIS