ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / GraphicsLayerExtensions Class / AddElement Method / AddElement(GraphicsLayer,Geometry,CIMSymbol,String,Boolean,ElementInfo) Method
The geometry to be used for the graphic element
The symbol to be used for the graphic element. Can be null (optional)
An element name (optional)
True to set the element selected after the add (default is true) (optional)
Additional element properties or null (optional)
Example

In This Topic
    AddElement(GraphicsLayer,Geometry,CIMSymbol,String,Boolean,ElementInfo) Method
    In This Topic
    Add a ArcGIS.Desktop.Layouts.GraphicElement based on the input geometry and symbol (optional). This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Overloads Shared Function AddElement( _
       ByVal graphicsLayer As GraphicsLayer, _
       ByVal geometry As Geometry, _
       Optional ByVal symbol As CIMSymbol, _
       Optional ByVal elementName As String, _
       Optional ByVal select As Boolean, _
       Optional ByVal elementInfo As ElementInfo _
    ) As GraphicElement

    Parameters

    graphicsLayer
    geometry
    The geometry to be used for the graphic element
    symbol
    The symbol to be used for the graphic element. Can be null (optional)
    elementName
    An element name (optional)
    select
    True to set the element selected after the add (default is true) (optional)
    elementInfo
    Additional element properties or null (optional)

    Return Value

    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    geometry cannot be null or empty
    symbol is not a CIMPointSymbol | CIMLineSymbol | CIMPolygonSymbol
    Map has reached maximum graphics count limit of 4000 elements. One or more elements cannot be created.
    Map has reached maximum graphics size limit of 10 MB. One or more elements cannot be created.
    Remarks
    Multipatch geometry is not currently supported.
    The geometry cannot be empty or null. Providing the wrong symbol type for the given geometry will result in an System.ArgumentException. If symbol is null a default symbol of the correct type will be assigned.
    The map view displaying the graphics layer should be initialized.
    Example
    Point Graphic Element using CIMGraphic
        var graphicsLayer = MapView.Active.Map.GetLayersAsFlattenedList()
    .OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();
        if (graphicsLayer == null)
          return;
        QueuedTask.Run(() =>
        {
          //Place symbol in the center of the map
          var extent = MapView.Active.Extent;
          var location = extent.Center;
    
          //specify a symbol
          var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
                                ColorFactory.Instance.GreenRGB);
    
          //create a CIMGraphic 
          var graphic = new CIMPointGraphic()
          {
            Symbol = pt_symbol.MakeSymbolReference(),
            Location = location //center of map
          };
          graphicsLayer.AddElement(graphic);
        });
    Line Graphic Element using CIMGraphic
    //On the QueuedTask
    //Place a line symbol using the extent's lower left and upper right corner.
    var extent = MapView.Active.Extent;
    //get the lower left corner of the extent
    var pointFromCoordinates = new Coordinate2D(extent.XMin, extent.YMin);
    //get the upper right corner of the extent
    var pointToCoordinates = new Coordinate2D(extent.XMax, extent.YMax);
    List<Coordinate2D> points = new List<Coordinate2D> { pointFromCoordinates, pointToCoordinates };
    //create the polyline
    var lineSegment = PolylineBuilderEx.CreatePolyline(points);
    
    //specify a symbol
    var line_symbol = SymbolFactory.Instance.ConstructLineSymbol(
                          ColorFactory.Instance.GreenRGB);
    
    //create a CIMGraphic 
    var graphic = new CIMLineGraphic()
    {
      Symbol = line_symbol.MakeSymbolReference(),
      Line = lineSegment,
    };
    graphicsLayer.AddElement(graphic);
    Polygon Graphic Element using CIMGraphic
    //On the QueuedTask
    //Place a polygon symbol using the mapview extent geometry
    var extent = MapView.Active.Extent;
    //Contract the extent
    var polygonEnv = extent.Expand(-100000, -90000, false);
    //create a polygon using the envelope
    var polygon = PolygonBuilderEx.CreatePolygon(polygonEnv);
    
    //specify a symbol
    var poly_symbol = SymbolFactory.Instance.ConstructPolygonSymbol(
                          ColorFactory.Instance.GreenRGB);
    
    //create a CIMGraphic 
    var graphic = new CIMPolygonGraphic()
    {
      Symbol = poly_symbol.MakeSymbolReference(),
      Polygon = polygon,
    };
    graphicsLayer.AddElement(graphic);
    Multi-point Graphic Element using CIMGraphic
    //On the QueuedTask
    //Place a multipoint graphic using the mapview extent geometry
    var extent = MapView.Active.Extent;
    //Contract the extent
    var polygonEnv = extent.Expand(-100000, -90000, false);
    //create a polygon using the envelope
    var polygon = PolygonBuilderEx.CreatePolygon(polygonEnv);
    //Create MultipPoints from the polygon
    var multiPoints = MultipointBuilderEx.CreateMultipoint(polygon);
    //specify a symbol
    var point_symbol = SymbolFactory.Instance.ConstructPointSymbol(
                          ColorFactory.Instance.GreenRGB);
    
    //create a CIMGraphic 
    var graphic = new CIMMultipointGraphic
    {
      Symbol = point_symbol.MakeSymbolReference(),
      Multipoint = multiPoints
    };
    graphicsLayer.AddElement(graphic);
    Graphic Element using CIMSymbol
        var graphicsLayer = MapView.Active.Map.GetLayersAsFlattenedList()
    .OfType<ArcGIS.Desktop.Mapping.GraphicsLayer>().FirstOrDefault();
        if (graphicsLayer == null)
          return;
        QueuedTask.Run(() =>
        {
          //Place symbol in the center of the map
          var extent = MapView.Active.Extent;
          var location = extent.Center;
    
          //specify a symbol
          var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
                                ColorFactory.Instance.GreenRGB);        
          graphicsLayer.AddElement(location, pt_symbol);
        });
    Translates a point in page coordinates to a point in map coordinates.
    //On the QueuedTask
    var layout = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault().GetLayout();
    var mapFrame = layout.FindElement("New Map Frame") as MapFrame;
    
    //Get a point in the center of the Map frame
    var mapFrameCenterPoint = mapFrame.GetBounds().CenterCoordinate;
    //Convert to MapPoint
    //At 2.x - var pointInMapFrame = MapPointBuilder.CreateMapPoint(mapFrameCenterPoint);
    var pointInMapFrame = MapPointBuilderEx.CreateMapPoint(mapFrameCenterPoint);
    
    //Find the corresponding point in the MapView
    var pointOnMap = mapFrame.PageToMap(pointInMapFrame);
    
    //Create a point graphic on the MapView.
    var cimGraphicElement = new CIMPointGraphic
    {
      Location = pointOnMap,
      Symbol = pointSymbol.MakeSymbolReference()
    };
    graphicsLayer.AddElement(cimGraphicElement);
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also