Common_TimerRedraw_CSharp\SimpleUpdate.aspx.cs
// Copyright 2010 ESRI // // All rights reserved under the copyright laws of the United States // and applicable international laws, treaties, and conventions. // // You may freely redistribute and use this sample code, with or // without modification, provided you include the original copyright // notice and use restrictions. // // See the use restrictions. // public partial class SimpleUpdate : System.Web.UI.Page { #region Member Variables // Name of the resource to add graphics to private string _graphicsResourceName = "GraphicsResource"; // Flag indicating whether one type of graphics is still visible when another type is cleared private bool _graphicsShown = false; // Array containing the names of the 48 contiguous states. Used in creating feature graphics for a // random subset of these states. private string[] _states = { "Alabama","Arizona","Arkansas","California","Colorado","Connecticut", "Delaware","Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana", "Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana", "Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina", "North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota", "Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming" }; #endregion #region Event Handlers - Page_Load, RequestReceived protected void Page_Load(object sender, System.EventArgs e) { // Add a handler for the PostbackManager's RequestReceived event. This event fires whenever doAsyncRequest // is called on on the client tier PostbackManager. For more information on the PostbackManager, see the // Common_PostbackManager sample. PostbackManager1.RequestReceived += new PostbackManager_CSharp.RequestReceivedEventHandler(PostbackManager1_RequestReceived); } // Fires when a request is received that was initiated by a call to PostbackManager.doAsyncRequest on the client void PostbackManager1_RequestReceived(object sender, PostbackManager_CSharp.AdfRequestEventArgs args) { // Parse the request arguments System.Collections.Specialized.NameValueCollection requestArgs = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments); // Check the event argument and draw or clear graphics accordingly switch (requestArgs["EventArg"]) { case "DrawFeatureGraphics": DrawFeatureGraphics(); break; case "DrawElementGraphics": DrawElementGraphics(); break; case "DrawAllGraphics": DrawFeatureGraphics(); DrawElementGraphics(); break; case "ClearFeatureGraphics": ClearGraphics("Feature"); break; case "ClearElementGraphics": ClearGraphics("Element"); break; } // Apply the graphics updates to the map Map1.RefreshResource(_graphicsResourceName); PostbackManager1.CallbackResults.CopyFrom(Map1.CallbackResults); // Create a callback result to hide the activity indicator ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult hideIndicatorCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("hideActivityIndicator();"); PostbackManager1.CallbackResults.Add(hideIndicatorCallbackResult); // Check whether graphics were created if (requestArgs["EventArg"].Contains("Draw") || _graphicsShown) { // Create callback result that will issue an update graphics request to the server in the update // interval specified on the UI ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult updateGraphicsCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("setGraphicsTimeout();"); PostbackManager1.CallbackResults.Add(updateGraphicsCallbackResult); } } #endregion #region Private Methods #region Graphics Manipulation - DrawFeatureGraphics, DrawElementGraphics, ClearGraphics // Draws a random subset of states as feature graphics on the map private void DrawFeatureGraphics() { // Get the graphics resource ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality graphicsMapFunctionality = Map1.GetFunctionality(_graphicsResourceName) as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality; // Get a query functionality for the USA_Data resource string targetResourceName = "USA"; ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality commonMapFunctionality = Map1.GetFunctionality(targetResourceName); ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality queryFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)commonMapFunctionality.Resource.CreateFunctionality( typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null); // Get the names and IDs of the layers in the resource that can be queried string[] layerIDs; string[] layerNames; queryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames); // Get the index of the states layer string targetLayerName = "States"; int targetLayerIndex = 0; for (int i = 0; i < layerNames.Length; i++) { if (layerNames[i].ToLower() == targetLayerName.ToLower()) { targetLayerIndex = i; break; } } // Initialize a filter for querying states ESRI.ArcGIS.ADF.Web.QueryFilter adfQueryFilter = new ESRI.ArcGIS.ADF.Web.QueryFilter(); adfQueryFilter.ReturnADFGeometries = true; adfQueryFilter.MaxRecords = 50; // Specify that only the STATE_NAME field be queried string targetFieldName = "STATE_NAME"; ESRI.ArcGIS.ADF.StringCollection stringCollection = new ESRI.ArcGIS.ADF.StringCollection(targetFieldName, ','); adfQueryFilter.SubFields = stringCollection; System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); // Generate the number of states to display graphics for System.Random randomizer = new System.Random(); int numberStates = randomizer.Next(4, 26); // Get a state name from the list string stateName = _states[randomizer.Next(_states.Length)]; // Add the number of unique state names specified by numberStates for (int i = 0; i < numberStates; i++) { // Get the list string stateList = stringBuilder.ToString(); // Keep picking random state names until one is picked that isn't on the list while (stateList.Contains(stateName)) stateName = _states[randomizer.Next(_states.Length)]; // Add the state to the list stringBuilder.AppendFormat("'{0}',", stateName); } // Remove the trailing comma from the list string whereClause = stringBuilder.ToString(); whereClause = whereClause.Substring(0, whereClause.Length - 1); // Specify that the query filter get features that match the states in the list adfQueryFilter.WhereClause = string.Format("STATE_NAME IN ({0})", whereClause); // Execute query System.Data.DataTable resultsTable = queryFunctionality.Query( commonMapFunctionality.Name, layerIDs[targetLayerIndex], adfQueryFilter); // Convert results to a graphics layer and add to the map ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer resultsGraphicsLayer = ESRI.ArcGIS.ADF.Web.UI.WebControls.Converter.ToGraphicsLayer(resultsTable); string layerName = "Feature Graphics"; resultsGraphicsLayer.TableName = layerName; if (graphicsMapFunctionality.GraphicsDataSet.Tables.Contains(layerName)) graphicsMapFunctionality.GraphicsDataSet.Tables.Remove(layerName); graphicsMapFunctionality.GraphicsDataSet.Tables.Add(resultsTable); } // Draws a set of random element graphic points on the map private void DrawElementGraphics() { // Get the map functionality for the graphics resource that will hold the element graphics ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality graphicsMapFunctionality = Map1.GetFunctionality(_graphicsResourceName) as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality; // Retrieve or create the element graphics layer string layerName = "Element Graphics"; ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null; if (graphicsMapFunctionality.GraphicsDataSet.Tables.Contains(layerName)) { elementGraphicsLayer = graphicsMapFunctionality.GraphicsDataSet.Tables[layerName] as ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer; // Remove graphics from the layer elementGraphicsLayer.Clear(); } else { // Create a new element graphics layer and add it to the map elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer(); elementGraphicsLayer.TableName = layerName; graphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer); } // Add a random number of points to add to the layer System.Random randomizer = new System.Random(); int numberPoints = randomizer.Next(4, 20); for (int i = 0; i < numberPoints; i++) { // Get a point that is randomly placed within the map's extent ESRI.ArcGIS.ADF.Web.Geometry.Point adfPoint = GetRandomPoint(Map1.Extent, randomizer); // Create a marker symbol with a random color, size, and symbol type ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol markerSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol(); markerSymbol.Color = GetRandomColor(randomizer); markerSymbol.Width = randomizer.Next(10, 30); markerSymbol.Type = (ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType)randomizer.Next(0, 5); // Use the point and symbol to create a graphic element ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPoint, markerSymbol); // Add the graphic to the layer elementGraphicsLayer.Add(graphicElement); } } // Removes element or feature graphics from the map private void ClearGraphics(string featureType) { // Get the graphics resource ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality graphicsMapFunctionality = Map1.GetFunctionality(_graphicsResourceName) as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality; // Remove the element or feature graphics layer from the resource string graphicsLayerName = string.Format("{0} Graphics", featureType); if (graphicsMapFunctionality.GraphicsDataSet.Tables.Contains(graphicsLayerName)) graphicsMapFunctionality.GraphicsDataSet.Tables.Remove(graphicsLayerName); // Set flag indicating whether there are still graphics visible. This will be used to determine whether // to create a callback to set a new graphics update timeout. _graphicsShown = (graphicsMapFunctionality.GraphicsDataSet.Tables.Count > 0); } #endregion #region Random Helper Methods - GetRandomColor, GetRandomPoint // Generates a random color private System.Drawing.Color GetRandomColor(System.Random randomizer) { byte[] rgb = new byte[3]; randomizer.NextBytes(rgb); return System.Drawing.Color.FromArgb(rgb[0], rgb[1], rgb[2]); } // Generates a random point within the given extent private ESRI.ArcGIS.ADF.Web.Geometry.Point GetRandomPoint(ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope, System.Random randomizer) { double x = adfEnvelope.XMin + randomizer.NextDouble() * adfEnvelope.Width; double y = adfEnvelope.YMin + randomizer.NextDouble() * adfEnvelope.Height; return new ESRI.ArcGIS.ADF.Web.Geometry.Point(x, y); } #endregion #endregion }