⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajaxmapcontrol.cs

📁 C# 的地图开发例子(sharp map)
💻 CS
📖 第 1 页 / 共 2 页
字号:
			set { _ResponseFormat = value; }
		}

		private bool _DisplayStatusBar;
		/// <summary>
		/// Specifies whether the statusbar is visible or not.
		/// </summary>
		[Bindable(false)]
		[Category("Appearance")]
		[DefaultValue(true)]
		[Description("Specifies whether the statusbar is visible or not.")]
		public bool DisplayStatusBar
		{
			get { return _DisplayStatusBar; }
			set { _DisplayStatusBar = value; }
		}
	
		/// <summary>
		/// Initializes a new instance of the <see cref="AjaxMapControl"/>
		/// </summary>
		public AjaxMapControl()
		{
			ZoomSpeed = 15;
			FadeSpeed = 10;
			_DisplayStatusBar = true;
		}
	
		/// <summary>
		/// The <see cref="SharpMap.Map"/> that is to be rendered in the control
		/// </summary>
		[Bindable(false)]
		[Category("Data")]
		[DefaultValue("")]
		[Localizable(true)]
		[Description("The map instance that is to be rendered in the control")]
		public SharpMap.Map Map
		{
			get { return map; }
			set { map = value; }
		}

		#region ICallbackEventHandler Members

		private string callbackArg = "";

		/// <summary>
		/// Returns the result of the callback event that targets <see cref="SharpMap.Web.UI.Ajax.AjaxMapControl"/>
		/// </summary>
		/// <returns></returns>
		public string GetCallbackResult()
		{
			EnsureChildControls();
			if (callbackArg.Trim() == "") return String.Empty;
			string[] vals = callbackArg.Split(new char[] { ';' });
			try
			{
				map.Zoom = double.Parse(vals[2], numberFormat_EnUS);
				map.Center = new SharpMap.Geometries.Point(double.Parse(vals[0], numberFormat_EnUS), double.Parse(vals[1], numberFormat_EnUS));
				map.Size= new System.Drawing.Size(int.Parse(vals[3]), int.Parse(vals[4]));
				return GenerateMap();
				//If you want to use the Cache for storing the map, instead of a maphandler,
				//uncomment the following lines, and comment the above return statement
				/*System.Drawing.Image img = map.GetMap();
				string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
				return "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);*/
			}
			catch { return String.Empty; }
		}

		/// <summary>
		/// Creates the arguments for the callback handler in the
		/// <see cref="System.Web.UI.ClientScriptManager.GetCallbackEventReference(System.Web.UI.Control,string,string,string)"/> method. 
		/// </summary>
		/// <param name="eventArgument"></param>
		public void RaiseCallbackEvent(string eventArgument)
		{
			callbackArg = eventArgument;
		}

		/// <summary>
		/// Sends server control content to a provided HtmlTextWriter object, which writes the content to be rendered on the client.
		/// </summary>
		/// <param name="writer">The HtmlTextWriter object that receives the server control content.</param>
		protected override void Render(HtmlTextWriter writer)
		{			
				base.Render(writer);
		}

		/// <summary>
		/// Called by the ASP.NET page framework to notify server controls that use
		/// composition-based implementation to create any child controls they
		/// contain in preparation for posting back or rendering
		/// </summary>
		protected override void CreateChildControls()
		{
			if (!Page.IsCallback)
			{
				GenerateMapBox();
				GenerateClientScripts();
			}
			//base.CreateChildControls();
		}

		/// <summary>
		/// Returns a Url to the map
		/// </summary>
		private string GenerateMap()
		{
			if (_UseCache)
			{
				System.Drawing.Image img = Map.GetMap();
				string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
				return "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);
			}
			else
				return _ResponseFormat.Replace("[WIDTH]", map.Size.Width.ToString()).
									  Replace("[HEIGHT]", map.Size.Height.ToString()).
									  Replace("[ZOOM]", map.Zoom.ToString(numberFormat_EnUS)).
									  Replace("[X]", map.Center.X.ToString(numberFormat_EnUS)).
									  Replace("[Y]", map.Center.Y.ToString(numberFormat_EnUS));
		}

		/// <summary>
		/// Registers the client-side scripts and creates an initialize script for the current map
		/// </summary>
		private void GenerateClientScripts()
		{
			string newline = Environment.NewLine;
			//Include scriptresource
			string scriptLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "SharpMap.UI.Web.UI.Ajax.AjaxMap.js");
			Page.ClientScript.RegisterClientScriptInclude("SharpMap.UI.AjaxMap.js", scriptLocation);
			string obj = this.ClientID + "Obj";
			string setvarsScript = "SetVars_" + this.ClientID + "();" + newline + 
				"function SetVars_" + this.ClientID + "() {" + newline +
				obj + " = SharpMap_Init('" + this.ClientID + "','"
				+ imgMap1.ClientID + "','" + imgMap2.ClientID + "','" + (_DisplayStatusBar ? spanCursorLocation.ClientID : "") + "','"+
				(_DisplayStatusBar ? _StatusBarText : "") + "','"+this.UniqueID+"');" + newline;
			setvarsScript +=
				obj + ".zoom = " + map.Zoom.ToString(numberFormat_EnUS) + ";" + newline +
				obj + ".minX = " + map.Envelope.Left.ToString(numberFormat_EnUS) + ";" + newline +
				obj + ".maxY = " + map.Center.Y.ToString(numberFormat_EnUS) + "+" + obj + ".zoom/" + obj + ".container.offsetWidth*" + obj + ".container.offsetHeight*0.5;" + newline +
				obj + ".minZoom = " + map.MinimumZoom.ToString(numberFormat_EnUS) + ";" + newline +
				obj + ".maxZoom = " + map.MaximumZoom.ToString(numberFormat_EnUS) + ";" + newline +
				obj + ".zoomAmount = 3.0;" + newline +
				obj + ".zoomSpeed = " + this._ZoomSpeed.ToString() +";" + newline +
				obj + ".fadeSpeed = " + this._FadeSpeed.ToString() + ";" + newline;
				
			if (_UseCache)
				setvarsScript += obj + ".map1.src = '" + this.GenerateMap() +"';\r\n";
			else
				setvarsScript += obj + ".map1.src = '" + _ResponseFormat.Replace("[WIDTH]", "'+" + obj + ".container.offsetWidth+'").
									  Replace("[HEIGHT]", "'+" + obj + ".container.offsetHeight+'").
									  Replace("[ZOOM]", "'+" + obj + ".zoom+'").
									  Replace("[X]", map.Center.X.ToString(numberFormat_EnUS)).
									  Replace("[Y]", map.Center.Y.ToString(numberFormat_EnUS)) + "';\r\n";
			
			if (!String.IsNullOrEmpty(_OnViewChange))
				setvarsScript += obj + ".onViewChange = function() { " + _OnViewChange + "(" + obj + "); }" + newline;
			if (!String.IsNullOrEmpty(_OnViewChanging))
				setvarsScript += obj + ".onViewChanging = function() { " + _OnViewChanging + "(" + obj + "); }" + newline;
			if (!String.IsNullOrEmpty(this._OnClickEvent))
				setvarsScript += this.ClickEventPropertyName + " = function(event) { " + this.OnClickEvent + "(event," + obj + ");};";			
			
			//setvarsScript += "SharpMap_BeginRefreshMap(" + obj + ",1);" + newline;

			setvarsScript += "}";

			//Register scripts in page
			ClientScriptManager cm = Page.ClientScript;
			//cm.RegisterClientScriptBlock(this.GetType(), "SetVars_" + this.ClientID, setvarsScript, true);
			cm.RegisterStartupScript(this.GetType(), "SetVars_" + this.ClientID, setvarsScript, true);
			//The following doesn't really do anything, but it cheats ASP.NET to include its callback scripts
			cm.GetCallbackEventReference(this, "SharpMap_MapOnClick(event,this)", "SharpMap_RefreshMap", "null", "SharpMap_AjaxOnError", true);

			//this.Controls.Add(new LiteralControl("<script type=\"text/javascript\">SetVars_" + this.ClientID + "();</script>\r\n"));
		}

		private void GenerateMapBox()
		{
			this.Style.Add("overflow", "hidden");
			this.Style.Add("z-index", "101");
			this.Style.Add("cursor", "pointer");
			this.Style.Add("position", "relative");
			this.Style.Add("display", "block");
			if(this.Style["BackColor"]!=null)
				this.Style.Add("background", System.Drawing.ColorTranslator.ToHtml(map.BackColor));

			imgMap1 = new System.Web.UI.WebControls.Image();
			imgMap2 = new System.Web.UI.WebControls.Image();
			imgMap1.Attributes["galleryimg"] = "false"; //Disable Internet Explorer image toolbar
			imgMap2.Attributes["galleryimg"] = "false"; //Disable Internet Explorer image toolbar						
			
			imgMap1.Style.Add("position", "absolute");
			imgMap1.Style.Add("Z-index", "10");
			imgMap2.Style.Add("position", "absolute");
			imgMap2.Style.Add("visibility", "hidden");
			imgMap2.Style.Add("opacity","0");
			imgMap2.Style.Add("filter","'ALPHA(opacity=0)'");
			imgMap2.Style.Add("Z-index","9");

			this.Controls.Add(imgMap1);
			this.Controls.Add(imgMap2);

			if (_DisplayStatusBar)
			{
				spanCursorLocation = new HtmlGenericControl("span");
				spanCursorLocation.InnerText = "";
				spanCursorLocation.Style.Add("filter", "ALPHA(opacity=100)");
				divTopBar = new HtmlGenericControl("div");
				divTopBar.Style.Clear();
				
				divTopBar.Style.Add("Z-index", "20");
				divTopBar.Style.Add("border-bottom", "1px solid #000");
				divTopBar.Style.Add("position", "absolute");
				divTopBar.Style.Add("filter", "ALPHA(opacity=50)");
				divTopBar.Style.Add("opacity ", "0.5");
				divTopBar.Style.Add("background", "#fff");
				divTopBar.Style.Add("width", "100%");

				divTopBar.Controls.Add(spanCursorLocation);
				this.Controls.Add(divTopBar);
			}
		}

		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -