Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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;
share|improve this answer
    
Yes, great, this is the GetGeometries-part I was missing in fencliffs example! Thanks!! –  Sam Feb 28 '11 at 15:04
add comment

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.

share|improve this answer
    
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 Feb 28 '11 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 Feb 28 '11 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 Feb 28 '11 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 Feb 28 '11 at 14:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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