I'm using the ArcGIS Runtime SDK 100.1.0 for .NET. I have a mobile map package (.mmpk) vector map and want to limit the maximum and minimum zoom in the MapView. I tried to track the MapScale property and set it:
((INotifyPropertyChanged)MyMapView).PropertyChanged += (sender, args) =>
{
args.PropertyName;
var s = MyMapView.MapScale;
if(s < 500)
MyMapView.SetViewpointScaleAsync(700);
if (s > 16500000)
MyMapView.SetViewpointScaleAsync(16500000);
};
This works, but map jerks at low/high zoom levels because it tries to smooth zoom and I can't figure out how to stop the active zooming task. What is the right way to do this?
If you just want to set a minimum scale and a maximum scale, the Map
class has MinScale
and MaxScale
properties. Replace your code with the following:
MyMapView.Map.MaxScale = 700;
MyMapView.Map.MinScale = 16500000;
You probably don't need this! Use the easy way listed above unless you have a good reason to do something more complicated!
If instead for some reason you really want to track scale changes and then change the scale yourself, you should do it in a different way. Currently you're listening for PropertyChanged
, which is way too broad. One effect is that when the scale changes, your event handler runs, which performs an asynchronous zoom, which generates a scale change before it's finished, which calls your event handler, which performs an asynchronous zoom, which generates a scale change before it's finished, which calls your event handler, which...I could go on and on. Literally. And so will your program, unless you make some changes.
Here's one way to do it:
// Save a variable so you can invoke the EventHandler elsewhere
EventHandler navigationCompletedHandler = (sender, args) =>
{
var s = MyMapView.MapScale;
if (s < 500)
MyMapView.SetViewpointScaleAsync(700);
if (s > 16500000)
MyMapView.SetViewpointScaleAsync(16500000);
};
MyMapView.NavigationCompleted += navigationCompletedHandler;
// Invoke the above handler one time when the map first loads
EventHandler firstViewpointChangeHandler = null;
firstViewpointChangeHandler = (sender, args) =>
{
if (!double.IsNaN(MyMapView.MapScale))
{
MyMapView.ViewpointChanged -= firstViewpointChangeHandler;
navigationCompletedHandler.Invoke(null, null);
}
};
MyMapView.ViewpointChanged += firstViewpointChangeHandler;