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


How to add a layer to a map from a layer file (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 a layer to a map from a layer file

How to add a layer to a map from a layer file


Summary
The following code example shows how to add a layer from a layer file (*.lyr) to a map. The subroutine takes an active view as input and adds the layer to the focus map of the active view. To use your own layer, change the path to your layer's path where specified.

In this topic


Adding a layer to a map from a layer file via GxLayer

Layer files are a common way to distribute cartographic properties for a dataset. In this section, the GxLayer class is used to open a layer and add it to a map. GxLayer is commonly used in ArcGIS Desktop to interact with layer files.
Complete the following steps to add a layer to a map from a layer file:
  1. Create a GxLayer and initialize its path property to the location where the applicable layer file is stored. See the following code example:
[C#]
//Create a GxLayer.
ESRI.ArcGIS.Catalog.IGxLayer gxLayerCls=new ESRI.ArcGIS.Catalog.GxLayer();
ESRI.ArcGIS.Catalog.IGxFile gxFile=(ESRI.ArcGIS.Catalog.IGxFile)LayerCls; 
    //Explicit Cast.

//Set the path where the layer file is located on disk.
gxFile.Path=layerPathFile;
[VB.NET]
'Create a GxLayer.
Dim gxLayerCls As ESRI.ArcGIS.Catalog.IGxLayer=New ESRI.ArcGIS.Catalog.GxLayer
Dim gxFile As ESRI.ArcGIS.Catalog.IGxFile=gxLayerCls 'Implicit Cast.

'Set the path where the layer file is located on disk.
gxFile.Path=layerPathFile
  1. Test if you have a valid GxLayer, then add it to the map. See the following code example:
[C#]
//Test if you have a valid layer and add it to the map.
if (!(gxLayerCls.Layer == null))
{
    ESRI.ArcGIS.Carto.IMap map=activeView.FocusMap;
    map.AddLayer(gxLayerCls.Layer);
}
[VB.NET]
'Test if you have a valid layer and add it to the map.
If Not gxLayerCls.Layer Is Nothing Then
    Dim map As ESRI.ArcGIS.Carto.IMap=activeView.FocusMap
    map.AddLayer(gxLayerCls.Layer)
End If

End Sub

Adding a layer to a map from a layer file via MapDocument

This operation is performed using the MapDocumentClass, which can open layer files, as well as map documents (.mxds) and published map files (.pmfs). MapDocument is available in ArcGIS Desktop, ArcGIS for Server, and ArcGIS Engine. 
  1. Create a MapDocument and use it to open the layer. See the following code example:
[C#]
IMapDocument pMapDocument=new MapDocumentClass();
//Use the map document class to open the layer.
pMapDocument.Open(layerPathFile, "");
[VB.NET]
Dim pMapDocument As IMapDocument=New MapDocumentClass()
'Use the map document class to open the layer.
pMapDocument.Open(layerPathFile, "")
  1. Add the layer to the map. See the following code example:
[C#]
//When opening a layer with the map document class,
//the layer is added to a new document as the first
//layer in the first map.
pMap.AddLayer(pMapDocument.get_Map(0).get_Layer(0));
[VB.NET]
'When opening a layer with the map document class,
'the layer is added to a new document as the first
'layer in the first map.
pMap.AddLayer(pMapDocument.get_Map(0).get_Layer(0))






Additional Requirements
  • activeView - An IActiveView referencing the map to which you are adding the layer.
  • pMap - An IMap referencing the map to which you are adding the layer.
  • layerPathFile - A string that contains the path to the layer file.

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