[GIS] How to zoom to an ILayer

arcobjectszoom

I used IFeatureLayerDefinition.CreateSelectionLayer() to create a new IFeatureLayer then i cast this one to ILayer and set it to the Map, my problem is that i can't zoom programmatically to this ILayer, i tried to call ICommand.ControlsMapFullExtentCommandClass but it didn't work.

This is my code :

public static void myFunction(ITable table_commune, string code_commune)
{

        if(table_commune == null || code_commune == null) return;

        ILayer layer = (ILayer)table_commune;
        IFeatureLayer featureLayer = (IFeatureLayer)layer;

        IFeatureLayerDefinition featureLayerDef = (IFeatureLayerDefinition)featureLayer;

        IFeatureLayer new_featureLayer = featureLayerDef.CreateSelectionLayer("Commune", false, null, "NUM_COMMUNE = " + (code_commune));
        ILayer new_layer = (ILayer)new_featureLayer;

        Map.AddLayer(new_layer);
        Map.ActiveView.Refresh();

        ICommand command = new ControlsMapFullExtentCommandClass();
        command.OnCreate(Map.Object);
        if(command.Enabled) command.OnClick();
}

Best Answer

You could first select the layer in the table of contents refrencing this snippet:

HowTo: Select a layer in the Table of Contents of ArcMap using ArcObjects

Then, zoom to the active layer (ILayer) referencing this snippet:

Zoom to Active Layer in TOC Snippet