[GIS] Zooming & panning to selected objects using ArcGIS Engine

arcgis-enginecwpf

I'm writing a program using ArcGIS Engine, and need to zoom & pan into the map to show the selected content.

Loading and displaying the map does work using something like this:

AxMapControl _mapControl;

// in constructor:
_mapControl = new AxMapControl();

// in loading
_mapControl.LoadMxFile(@"C:\Users\me\Documents\TestProject.mxd"); 

Afterwards I select some objects and want to zoom into them, but I can't find a way to get the selected extent – any pointers to achieve this?

Best Answer

suppose you have just one Layer, with the selected features & you want to zoom to the extent of all the selected features, here is how I would do it:

IMapControl2 MapControl= (ImapControl2)_mapControl;
IfeatureLayer FeatureLayer=MapControl.Layer[0];

IFeatureSelection FeatureSelection= (IFeatureSelection)FeatureLayer;

ISelectionSet SelectionSet=FeatureSelection.selectionSet;

ICursor cursor;
SelectionSet.Search(Null, true, out cursor);

IFeatureCursor featureCursor = (IFeatureCursor)cursor; 


IFeature feature;
IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();

while ((feature = featureCursor.NextFeature()) != null)
      {
        IGeometry geometry = feature.Shape;
        IEnvelope featureExtent = geometry.Envelope;
        envelope.Union(featureExtent);
}

MapControl.Extent=envelope;