This document is archived and information here might be outdated.  Recommended version.


How to add different types of layers to a map (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with and configuring maps, layers, and graphics > Working with layers and renderers > How to add different types of layers to a map

How to add different types of layers to a map


Adding different types of layers to a map

In ArcGIS, layer types often correspond to different geodatabase dataset types. When creating layers from database objects, it is important to create the correct layer type to ensure proper behavior. This topic shows how to create a layer and hook it up to the geodatabase dataset using the LayerFactoryHelper class. 
Do the following to add different types of layers to a map:
  1. The LayerFactoryHelper class leverages LayerFactory objects registered in the Layer Factory component category to create the proper layer type for the geodatabase Name object. The output layers are then added to the map. See the following code example:
[C#]
static void CreateLayerFromGeodatabaseName(IMap pMap, IName pName)
{
    ILayerFactoryHelper pLayerFactoryHelper=new LayerFactoryHelperClass();
    IEnumLayer pEnumLayer;
    //Layer Factory Helper creates the correct type of layer from the Name object
    //and returns the layer in an enumeration.
    pEnumLayer=pLayerFactoryHelper.CreateLayersFromName(pName);
    ILayer pLayer;
    pEnumLayer.Reset();
    pLayer=pEnumLayer.Next();
    // Loop through the enum and add each layer.
    while (pLayer != null)
    {
        pMap.AddLayer(pLayer);
        pLayer=pEnumLayer.Next();
    }
}
[VB.NET]
Private Sub CreateLayerFromGeodatabaseName(ByVal pMap As IMap, ByVal pName As IName)
    Dim pLayerFactoryHelper As ILayerFactoryHelper=New LayerFactoryHelperClass()
    Dim pEnumLayer As IEnumLayer
    'Layer Factory Helper creates the correct type of layer from the Name object
    'and returns the layer in an enumeration.
    pEnumLayer=pLayerFactoryHelper.CreateLayersFromName(pName)
    Dim pLayer As ILayer
    pEnumLayer.Reset()
    pLayer=pEnumLayer.Next()
    ' Loop through the enum and add each layer.
    While Not pLayer Is Nothing
        pMap.AddLayer(pLayer)
        pLayer=pEnumLayer.Next()
    End While
End Sub
  1. The following code example shows how to add a layer based on a shapefile to the map. It assumes that you already have m_application variable as a reference to the Application object in your .NET custom component.
[C#]
private void AddShapeFile()
{
    // Create a ShapefileWorkspaceFactory object and
    // open a shapefile folder. The path works with a standard 9.3 installation.
    IWorkspaceFactory workspaceFactory=new ShapefileWorkspaceFactoryClass();
    IFeatureWorkspace featureWorkspace=(IFeatureWorkspace)
        workspaceFactory.OpenFromFile(
        "C:\\Program Files\\ArcGIS\\DeveloperKit10.0\\Samples\\data\\Y2000HurricaneData", 0);
    // Create a FeatureLayer and assign a shapefile to it.
    IFeatureLayer featureLayer=new FeatureLayerClass();
    featureLayer.FeatureClass=featureWorkspace.OpenFeatureClass("2000_hrcn");
    ILayer layer=(ILayer)featureLayer;
    layer.Name=featureLayer.FeatureClass.AliasName;
    // Add the layer to the focus map.
    ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument=(ESRI.ArcGIS.ArcMapUI.IMxDocument)
        (m_application.Document);
    IMap map=mxDocument.FocusMap;
    map.AddLayer(layer);
}
[VB.NET]
Private Sub AddShapefile()
    Dim mapDoc As IMapDocument=m_application.Document
    ' Create a ShapefileWorkspaceFactory object and
    ' open a shapefile folder. The path works with a standard 9.3 installation.
    Dim workspaceFactory As IWorkspaceFactory=New ShapefileWorkspaceFactoryClass()
    Dim featureWorkspace As IFeatureWorkspace=TryCast(workspaceFactory.OpenFromFile("C:\\Program Files\\ArcGIS\\DeveloperKit10.0\\Samples\\data\\Y2000HurricaneData", 0), IFeatureWorkspace)
    ' Create a FeatureLayer and assign a shapefile to it.
    Dim featureLayer As IFeatureLayer=New FeatureLayerClass
    featureLayer.FeatureClass=featureWorkspace.OpenFeatureClass("2000_hrcn")
    Dim layer As ILayer=featureLayer
    layer.Name=featureLayer.FeatureClass.AliasName
    ' Add the layer to the focus map.
    Dim pMap As ESRI.ArcGIS.Carto.IMap=mapDoc.ActiveView.FocusMap
    pMap.AddLayer(featureLayer)
End Sub
  1. The following code example adds a new graphics layer to the map. If you run this program, then look on the Annotation groups tab of the data frame properties, you will see a new group, that is, a new graphics layer in the list. To draw in this new layer, make it the active graphics layer. This is done by setting the ActiveGraphicsLayer property of the map. You can tell which layer is the active graphics layer, by going to the active annotation target sub-menu on the Drawing menu in ArcMap.
[C#]
private void AddGraphicsLayer()
{
    IMapDocument mapDoc=(IMapDocument)m_application.Document;
    IMap map=mapDoc.ActiveView.FocusMap;
    ICompositeGraphicsLayer compositeGraphicsLayer=(ICompositeGraphicsLayer)
        map.BasicGraphicsLayer;
    IGraphicsLayer graphicsLayer=compositeGraphicsLayer.AddLayer(
        "My New Graphics Layer", null);
    map.ActiveGraphicsLayer=(ILayer)graphicsLayer;
}
[VB.NET]
Private Sub AddGraphicsLayer()
    Dim mapDoc As IMapDocument=m_application.Document
    Dim map As IMap=mapDoc.ActiveView.FocusMap
    Dim compositeGraphicsLayer As ICompositeGraphicsLayer=TryCast(map.BasicGraphicsLayer, ICompositeGraphicsLayer)
    Dim graphicsLayer As IGraphicsLayer=compositeGraphicsLayer.AddLayer("My New Graphics Layer", Nothing)
    map.ActiveGraphicsLayer=TryCast(graphicsLayer, ILayer)
End Sub






Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine