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 have a stand-alone application written in C#. I want to be able to save and restore the current map view (extent) that the user sees. I assumed that it would be as simple as saving the active view's extent and then later resetting it, but that doesn't seem to work.

Here is some code:

_myModel.Envelope = _axMapControl.ActiveView.Extent; // save it 

...

_axMapControl.Extent = _myModel.Envelope; // doesn't work
_axMapControl.ActiveView.Extent = _myModel.Envelope; // also doesn't work

_axMapControl.Refresh(); // doesn't do anything
_axMapControl.ActiveView.Refresh(); // doesn't do anything
_axMapControl.ActiveView.ContentsChanged();  // doesn't do anything

Note that after the assignments, both _axMapControl.Extent and _axMapControl.ActiveView.Extent values are unchanged.

share|improve this question
    
I would just grab the map scale and the center point... and restore with center at, and .MapScale –  patrick Feb 24 '12 at 20:58
    
Make it an answer and I'll accept it as the best answer. –  Keith G Feb 26 '12 at 3:44
    
I think I know another way too, I'll look at my code on Monday, might have a better answer for you. –  patrick Feb 26 '12 at 19:53
add comment

4 Answers

up vote 0 down vote accepted

Since there has been some activity on this question today, it reminded me that I never followed-up. Here is the implementation that I used:

To save ...

void _axMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
   UpdateMapViewState((IEnvelope) e.newEnvelope);
}


private void UpdateMapViewState(IEnvelope newEnvelope)
{
   IPoint pt = new Point();
   pt.X = ((newEnvelope.XMax - newEnvelope.XMin) / 2.0) + newEnvelope.XMin;
   pt.Y = ((newEnvelope.YMax - newEnvelope.YMin) / 2.0) + newEnvelope.YMin;

   _model.MapCenter = pt; 
   _model.MapScale = _axMapControl.MapScale;
}

To restore ...

_axMapControl.CenterAt(_model.MapCenter);
_axMapControl.MapScale = _model.MapScale;
_axMapControl.Refresh();
share|improve this answer
    
hmm, sounds familiar –  patrick Mar 23 '12 at 19:55
    
iterationx, I asked you to make your comment into an answer so that I could accept it as the correct one, but you never did. –  Keith G Mar 23 '12 at 20:44
    
gis.stackexchange.com/a/20917/733 –  patrick Mar 23 '12 at 20:47
add comment

Your _myModel.Envelope variable is just a reference (pointer) to the very same instance of _axMapControl.ActiveView.Extent. So when you try to set the extent on the mapcontrol it doesn't do anything, as _axMapControl.ActiveView.Extent already points to that envelope instance.

You have to perform a deep copy. I guess casting the mapcontrol's envelope to IClone interface and perform the Clone method should do the trick. Otherwise you could create a new envelopeclass object and assign the ymax,xmin,ymin and xmax attributes.

share|improve this answer
add comment

I don't do C# just VB but the code you have posted seems correct. It is certainly possible to to set the map to any extent you want.

We can't see the entire procedure so we can't really determine what's going on between the line of code where you obtain the current extent and the snippet below it where you are using the variable to set the same extent again.

You are setting the map extent to exactly the same extent so I would expect nothing would happen.

however:

...saving the active view's extent and then later resetting it...

If your variable is global then it will do exactly that. You can debug to make sure the envelope has the the original width and length just prior to setting the extent. You should set a break on this line:

_axMapControl.Extent = _myModel.Envelope;
share|improve this answer
add comment

I guess I do it like this:

m_mapkernel.MapControl.Extent = (IEnvelope)myEnv;

Maybe your IEnvelope object is somehow invalid or not projected properly.

You could also grab the map scale and the center point... and restore with .CenterAt, and .MapScale

share|improve this answer
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.