Limit arcgis .net SDK 100.1 zoom levels

638 views Asked by At

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?

2

There are 2 answers

0
Gary Sheppard On BEST ANSWER

The easy way

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;

The hard way

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

You can just set the zoom levels to whatever intervals you want in ArcMap for your map file, when you build your map package / publish it to a service ArcMap seems to honour it (does for me at least).

Map Scale Drop Down -> Customize This List -> Standard Scales -> Tick the box "Only display these scales when zooming"

Saves having to write a custom mapscale event handler.