PanTo and ZoomToResolution

2745
9
Jump to solution
10-20-2014 03:56 PM
IvanDemkovitch
New Contributor II

I have trouble getting Map behave properly when calling ZoomToResolution and PanTo

I need to be able to Zoom into specific coordinate and center map.

The only way I got it working is by removing animations:

this.MapControl.ZoomDuration = new TimeSpan(0);

this.MapControl.PanDuration = new TimeSpan(0);

Otherwise if I make call like this:

control.MapControl.ZoomToResolution(ZoomLevel);

control.MapControl.PanTo(MapPoint());

It does one or another (i.e. pan or zoom, but not both). If (after animation) I call this code second time (map already zoomed or panned to needed level) - it does second part.

Tried this:

control.MapControl.ZoomToResolution(ZoomLevel, MapPoint());

Same issue.

So, my only workaround right now is to set Zoom/Pan duration to 0. And it makes for bad UX when using mouse.

I also tried something like this:

this.MapControl.ZoomDuration = new TimeSpan(0);

this.MapControl.PanDuration = new TimeSpan(0);

control.MapControl.ZoomToResolution(ZoomLevel);

control.MapControl.PanTo(MapPoint());

this.MapControl.ZoomDuration = new TimeSpan(750);

this.MapControl.PanDuration = new TimeSpan(750);

Which seems to be working, but then mouse interaction becomes "crazy". Mouse scroll will make map jump and zoom to random places.

Is there known solution?

0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor

I just tested with a button that zoom and center to Paris with this line of code:

    private void CenterAndZoomToParis(object sender, RoutedEventArgs e)

    {

        CenterAndZoom(MyMap, new MapPoint(260000, 6250000), 20);

    }

Whatever the initial extent, by clicking on this button the map pans to the Paris center and zooms to the specified resolution at the same time.

Isn't it what you are looking for?

Code of CenterAndZoom in case we are not using the same one:

    private void CenterAndZoom(ESRI.ArcGIS.Client.Map map, ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint, double resolution)

    {

        double ratio = 1.0;

        if (map.Resolution != 0.0)

            ratio = resolution / map.Resolution;

        if (ratio == 1.0)

            map.PanTo(mapPoint);

        else

        {

            ESRI.ArcGIS.Client.Geometry.MapPoint mapCenter = map.Extent.GetCenter();

            double X = (mapPoint.X - ratio * mapCenter.X) / (1 - ratio);

            double Y = (mapPoint.Y - ratio * mapCenter.Y) / (1 - ratio);

            map.ZoomToResolution(resolution, new ESRI.ArcGIS.Client.Geometry.MapPoint(X, Y));

        }

    }

View solution in original post

0 Kudos
9 Replies
DominiqueBroux
Esri Frequent Contributor

ZoomTo and PanTo with animation are asynchronous operations so executing both at the same time may give unexpected behavior.

Please look at this thread for a possible workaround.

/Dominique

0 Kudos
IvanDemkovitch
New Contributor II

Workaround you provided does same thing. It doesn't handle Pan and Zoom at the same time. Works exactly the same as my 2 line code. Last command takes place. If second command not necessary (map already centered at X/Y) - then Zoom works (first command).

0 Kudos
DominiqueBroux
Esri Frequent Contributor

The goal of the workaround is to execute only one asynchronous command.

The provided method centers the map to a specified point with a specified resolution in one command.

0 Kudos
IvanDemkovitch
New Contributor II

It doesn't for me. If map is not at desired zoom/position than workaround calls:

map.ZoomToResolution(resolution, new ESRI.ArcGIS.Client.Geometry.MapPoint(X, Y));

And this command doesn't do both as internally it uses PanTo (for MapPoint override) so same result.

0 Kudos
DominiqueBroux
Esri Frequent Contributor

If 'resolution' argument of CenterAndZooom is not the current map resolution, ZoomToResolution should not call 'PanTo' internally. It should zoom to the new expected resolution.

0 Kudos
IvanDemkovitch
New Contributor II

Exactly. And then it's a same result. Map will Zoom to needed resolution but will not Pan where I want. I need to center and zoom map with one command. If map was at correct resolution already - it will Pan with this command. So, all I'm saying - resolution provided works the same as if just using ZoomToResolution with point.

0 Kudos
DominiqueBroux
Esri Frequent Contributor

I just tested with a button that zoom and center to Paris with this line of code:

    private void CenterAndZoomToParis(object sender, RoutedEventArgs e)

    {

        CenterAndZoom(MyMap, new MapPoint(260000, 6250000), 20);

    }

Whatever the initial extent, by clicking on this button the map pans to the Paris center and zooms to the specified resolution at the same time.

Isn't it what you are looking for?

Code of CenterAndZoom in case we are not using the same one:

    private void CenterAndZoom(ESRI.ArcGIS.Client.Map map, ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint, double resolution)

    {

        double ratio = 1.0;

        if (map.Resolution != 0.0)

            ratio = resolution / map.Resolution;

        if (ratio == 1.0)

            map.PanTo(mapPoint);

        else

        {

            ESRI.ArcGIS.Client.Geometry.MapPoint mapCenter = map.Extent.GetCenter();

            double X = (mapPoint.X - ratio * mapCenter.X) / (1 - ratio);

            double Y = (mapPoint.Y - ratio * mapCenter.Y) / (1 - ratio);

            map.ZoomToResolution(resolution, new ESRI.ArcGIS.Client.Geometry.MapPoint(X, Y));

        }

    }

0 Kudos
IvanDemkovitch
New Contributor II

Ok.. Yes, it works for me now. With a glitch. I can reproduce it 100%, but it's too much to post. I'm sure it's some kind of bug inside control. When I follow specific steps it wouldn't pan into location needed. But it happens only after start up. When I operate map manually with mouse - everything starts working correctly..

I'm taking this solution as it's very unlikely that it will cause issue for users..

Thank you!

IvanDemkovitch
New Contributor II


Solution provided here works:

Esri Silverlight control Pan/Zoom from code - Stack Overflow

But it doesn't animate..

0 Kudos