3

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?

2 Answers 2

4

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;
1
  • Yes, great, this is the GetGeometries-part I was missing in fencliffs example! Thanks!!
    – Sam
    Commented Feb 28, 2011 at 15:04
2

You could try this:

List<IGeometry> geometries = GetGeometries() // However you retrieve your geometries
double margin = 1.2; 
IEnvelope envelope = null;
foreach (IGeometry g in geometries)
{
    if (envelope == null)
        envelope = g.Envelope;
    else
        envelope.Union(g.Envelope);
}
envelope.Expand(margin , margin , true);
_mapControl.ActiveView.Extent = envelope;
_mapControl.ActiveView.Refresh();

You can retrieve the geometry of a feature from IFeature.Shape.

4
  • I'm not yet sure how to get from AxMapControl to GetGeometries (which usually is my main problem with ArcGis: not knowing how to get from where to there).
    – Sam
    Commented Feb 28, 2011 at 14:38
  • But at least your example looks like my old avenue code - except for the black magic part, where in avenue I used GetThemes > GetFTab > GetSelection to get the selected items.
    – Sam
    Commented Feb 28, 2011 at 14:42
  • @Sam GetGeometries() is just an placeholder for place your retrieval code here, not from my codebase :) I'll try to get back to you later when I have time, unless someone else has answered by then.
    – fencliff
    Commented Feb 28, 2011 at 14:49
  • Aah, ok, so I need to get my selected features into a list of IGeometries - I'll try my very best, thanks for your pointers!!
    – Sam
    Commented Feb 28, 2011 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.