Creating and using a custom MapGraphic layer

A GraphicLayer can be used to draw objects that are not in a feature class on the map display.

To create a custom GraphicLayer, you can use the generic class template, then edit the class file created. In Visual Studio, use the Project\Add Class menu to access the Add New item menu, where you can select the class template and choose an appropriate name. The class file is added to your solution automatically.

The following sample code snippets show the minimum code used to store and draw a series of points as a graphic layer. The sample referenced at the end of this topic expands this concept to work with different feature types and performs custom rendering of the graphics layer.

public class CustomGraphicLayer: MapGraphicLayer
{ 
/// Defines a symbol class instance for displaying point 
private Symbol m_pointSymbol;
/// Defines a CoordinateCollection instance to store custom features 
private CoordinateCollection m_coordinateCollection = new CoordinateCollection();
/// Get or set coordinate collection stored in custom graphic layer 
public CoordinateCollection Coordinates 
{
  get {
    return m_coordinateCollection;
  } 
  set {
    m_coordinateCollection = value;
  }
}
/// Initializes a new instance of custom graphic layer 
public CustomGraphicLayer(): base("CustomGraphicLayer") 
{
  m_pointSymbol = new Symbol( new PointPaintOperation( Color.LightGreen, 2, 0, Color.LightGreen, 50, 25, PointPaintStyle.Circle)); 
} 
/// Draw method being called after adding to map 
protected override void Draw(Display display) 
{
  //return if display is null 
  if (display == null) 
    return;
  //return if drawing is cancelled
  if (display.DrawingCanceled) 
    return;
  //draw coordinate collection using DrawMultipoint method
  m_pointSymbol.DrawMultipoint(display, m_coordinateCollection);
}

Use the following code to create an instance of this custom graphic layer. Once the class is instantiated, it can be added to the map in the same way as any other graphic layer.

// Initializes a custom graphic layer instance 
CustomGraphicLayer graphicLayer = new CustomGraphicLayer(); 
//Add custom graphic layer to MapGraphicLayers collection
map1.MapGraphicLayers.Add(graphicLayer);

Where to get the sample

The sample with additional functionality is available for download from ArcGIS.com.

1/7/2015