AddElement(GraphicsLayer,CIMGraphic,String,Boolean,ElementInfo) Method
Add a
ArcGIS.Desktop.Layouts.GraphicElement based on the
ArcGIS.Core.CIM.CIMGraphic. This method must be called on the MCT.Use QueuedTask.Run.
Translates a point in page coordinates to a point in map coordinates.
{
var pointSymbol = SymbolFactory.Instance.ConstructPointSymbol(ColorFactory.Instance.BlackRGB, 8);
var activeMapView = MapView.Active;
await QueuedTask.Run(() =>
{
var graphicsLayer = activeMapView.Map.GetLayersAsFlattenedList().OfType<GraphicsLayer>().FirstOrDefault();
//Get a point in the center of the Map frame
var mapFrameCenterPoint = mapFrame.GetBounds().CenterCoordinate;
//Convert to MapPoint
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);
});
}
Get the map clipping geometry
{
var mapWithClip = MapView.Active.Map;
await QueuedTask.Run(() =>
{
//If clipping is set to ArcGIS.Core.CIM.ClippingMode.None or ArcGIS.Core.CIM.ClippingMode.MapSeries null is returned
//If clipping is set to ArcGIS.Core.CIM.ClippingMode.MapExtent the ArcGIS.Core.CIM.CIMMap.CustomFullExtent is returned.
//Otherwise, if clipping is set to ArcGIS.Core.CIM.ClippingMode.CustomShape the custom clip polygon is returned.
var poly = mapWithClip.GetClipGeometry();
//You can use the polygon returned
//For example: We make a polygon graphic element and add it to a Graphics Layer.
var gl = mapWithClip.GetLayersAsFlattenedList().OfType<GraphicsLayer>().FirstOrDefault();
if (gl == null)
{
return;
}
var polygonSymbol = SymbolFactory.Instance.ConstructPolygonSymbol(CIMColor.CreateRGBColor(255, 255, 0));
var cimGraphicElement = new CIMPolygonGraphic
{
Polygon = poly,
Symbol = polygonSymbol.MakeSymbolReference()
};
gl.AddElement(cimGraphicElement);
});
}
Point Graphic Element using CIMGraphic
{
await QueuedTask.Run(() =>
{
//Place symbol in the center of the map
var extent = mapView.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
{
await QueuedTask.Run(() =>
{
//Place a line symbol using the extent's lower left and upper right corner.
var extent = mapView.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
{
await QueuedTask.Run(() =>
{
//Place a polygon symbol using the mapview extent geometry
var extent = mapView.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
{
await QueuedTask.Run(() =>
{
//Place a multipoint graphic using the mapview extent geometry
var extent = mapView.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);
});
}
Add the Most Recent Location To A Graphics Layer
{
await QueuedTask.Run(() =>
{
// get the last location
var pt = DeviceLocationService.Instance.GetCurrentSnapshot()?.GetPositionAsMapPoint();
if (pt != null)
{
//Create a point symbol
var ptSymbol = SymbolFactory.Instance.ConstructPointSymbol(
CIMColor.CreateRGBColor(125, 125, 0), 10, SimpleMarkerStyle.Triangle);
//Add a graphic to the graphics layer
graphicsLayer.AddElement(pt, ptSymbol);
//unselect it
graphicsLayer.ClearSelection();
}
});
}
Graphic Element using CIMSymbol
{
await QueuedTask.Run(() =>
{
//Place symbol in the center of the map
var extent = mapView.Extent;
var location = extent.Center;
//specify a symbol
var pt_symbol = SymbolFactory.Instance.ConstructPointSymbol(
ColorFactory.Instance.GreenRGB);
graphicsLayer.AddElement(location, pt_symbol);
});
}
Text Graphic Element
{
await QueuedTask.Run(() =>
{
//Place symbol in the center of the map
var extent = mapView.Extent;
var location = extent.Center;
//specify a text symbol
var text_symbol = SymbolFactory.Instance.ConstructTextSymbol
(ColorFactory.Instance.BlackRGB, 8.5, "Corbel", "Regular");
graphicsLayer.AddElement(location, text_symbol, "Text Example");
});
}
Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)
ArcGIS Pro version: 3.0 or higher.