protected void MapControl1_ViewBoundsChanged(object sender, ViewBoundsChangedEventArgs e) { /************************************************************************/ /* move the vml rect with the bounds change * */ /************************************************************************/ if (e.ViewBoundsBefore != null) { //get the width change rate named rate_x double CurrentWidth = e.ViewBoundsAfter.RightTop.X - e.ViewBoundsAfter.LeftBottom.X; double OldWidth = e.ViewBoundsBefore.RightTop.X - e.ViewBoundsBefore.LeftBottom.X; double rate_X = CurrentWidth / OldWidth; //get the height change rate named rate_y double CurrentHeight = e.ViewBoundsAfter.RightTop.Y - e.ViewBoundsAfter.LeftBottom.Y; double OldHeight = e.ViewBoundsBefore.RightTop.Y - e.ViewBoundsBefore.LeftBottom.Y; double rate_Y = CurrentHeight / OldHeight;
//用两对double类型的变量得到存放在Session里面的坐标信息 double Start_X = Convert.ToDouble(Session["Start_X"]); double Start_Y = Convert.ToDouble(Session["Start_Y"]); double End_X = Convert.ToDouble(Session["End_X"]); double End_Y = Convert.ToDouble(Session["End_Y"]);
//得到当前MapControl 控件的宽度和高度 string widthpx = this.MapControl1.Width.ToString(); string heightpx = this.MapControl1.Height.ToString(); //因为返回值为System.Web.UI.WebControls.Unit类型的 //需要得到具体的数值,然后进行计算 string strWidth = widthpx.Substring(0, widthpx.Length - 2); string strHeight = heightpx.Substring(0, heightpx.Length - 2);
//计算出新的Rect对象的宽度 double dVmlWidth = (End_X - Start_X) / (this.MapControl1.ViewBounds.LeftBottom.X - this.MapControl1.ViewBounds.RightTop.X) * Convert.ToDouble(strWidth); rate_X = Math.Round(rate_X, 5); if (rate_X > 1) { dVmlWidth = Math.Abs(dVmlWidth) / rate_X * 2; } else if (rate_X < 1) { dVmlWidth = Math.Abs(dVmlWidth) / rate_X / 2; } else if (rate_X == 1) { dVmlWidth = Math.Abs(dVmlWidth); }
//计算出新的Rect对象的宽度 double dVmlHeight = (End_Y - Start_Y) / (this.MapControl1.ViewBounds.RightTop.Y - this.MapControl1.ViewBounds.LeftBottom.Y) * Convert.ToDouble(strHeight); rate_Y = Math.Round(rate_Y, 5); if (rate_Y > 1) { dVmlHeight = Math.Abs(dVmlHeight) / rate_Y * 2; } else if (rate_Y < 1) { dVmlHeight = Math.Abs(dVmlHeight) / rate_Y / 2; } else if (rate_Y == 1) { dVmlHeight = Math.Abs(dVmlHeight); }
//for debug System.Diagnostics.Debug.WriteLine("dVmlWidth=" + dVmlWidth); System.Diagnostics.Debug.WriteLine("dVmlHeight=" + dVmlHeight);
//创建添加VML操作的innerHTML string strVML = ""; strVML += string.Format("<v:rect style='position:relative;width:{0}px;height:{1}px'>", dVmlWidth, dVmlHeight); strVML += "<v:fill opacity=0.3 color='#F95858'></v:fill>"; strVML += "<v:stroke dashstyle='solid' Color='#316AC5' weight='1px'></v:stroke>"; strVML += "</v:rect>";
//在MapControl的CustomLayer上面添加HTML代码 this.MapControl1.CustomLayer.Divs.Insert("vml", Start_X + (End_X - Start_X) / 2, End_Y - (End_Y - Start_Y) / 2, strVML); //设置MapControl的CustomLayer可见 this.MapControl1.CustomLayer.Visible = true; } }
|