Creating and using a custom MapGraphic layer

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

To create a new custom GraphicLayer you can use the generic class template and 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 example below shows some basic code which is used to store and draw a series of points as a graphic 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);
}

In order to create an instance of this custom graphic layer, use the code shown below. Once the class is instantiated it can be added to the map just like 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 is available for download from here.


9/20/2011