2

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.

2
  • I would just grab the map scale and the center point... and restore with center at, and .MapScale
    – patrick
    Commented Feb 24, 2012 at 20:58
  • I think I know another way too, I'll look at my code on Monday, might have a better answer for you.
    – patrick
    Commented Feb 26, 2012 at 19:53

4 Answers 4

1

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();
3
  • hmm, sounds familiar
    – patrick
    Commented Mar 23, 2012 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
    Commented Mar 23, 2012 at 20:44
  • gis.stackexchange.com/a/20917/733
    – patrick
    Commented Mar 23, 2012 at 20:47
0

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

0

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;
0

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.

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.