Feb 8, 2010

How to draw a Geometry on the Map + ArcGIS + .Net Web ADF

This is a common requirement in the Mapping Applications to Draw the People's Area of Interest(AOI) in different geometry forms like Rectangle, Polygon etc.

Below is the method that allow you draw the AOI on the map and zoom to it, when you pass the bounds as inputs.

public void DrawRectangleOnMap(double minX,double minY,double maxX,double maxY)
ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality adfGraphicsMapFunctionality = null;
ElementGraphicsLayer elementGraphicsLayer = null;
GraphicsDataSet graphicsDataSet = null;

foreach (IMapFunctionality mapFunctionality in Map1.GetFunctionalities())
// If multiple graphics resources are available, use the resource name to distinguish
if (mapFunctionality.Resource.Name == "ADFGraphicsResource")
adfGraphicsMapFunctionality = mapFunctionality as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality;

// Return if there is no Graphic Resource.
if (adfGraphicsMapFunctionality == null)
elementGraphicsLayer = new ElementGraphicsLayer();
elementGraphicsLayer.TableName = Guid.NewGuid().ToString();

 // Add graphics layer to map functionality graphics dataset
graphicsDataSet = adfGraphicsMapFunctionality.GraphicsDataSet;

// Create a Geometry using the bound values from the QueryString .
ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
 adfPolygon.Rings.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Ring
(new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minX, minY, maxX, maxY)));

 // Fill the Geometry with color and style.
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol simpleFillSymbol =
new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol();
simpleFillSymbol.Transparency = 100;
simpleFillSymbol.BoundaryColor = System.Drawing.Color.Red;

// Loads the Geometry to the GraphicElement.
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement =
new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPolygon, simpleFillSymbol);

// Add the GraphicElement to the GraphicsLayer.

// Zooms to the AOI
catch (Exception)
elementGraphicsLayer = null;
graphicsDataSet = null;
adfGraphicsMapFunctionality = null;

No comments: