I have gotten the "ZoomToResolution" method to work not with a slider, but as the user clicks a point or the user clicks a row in a "FeatureDataGrid". This is how I do it:
I use Bing Maps as a background; and it is Bing Maps resolution that guides all the values below.
1. I had to find out the map minimun resolution; mine is 0.0988998672921598. The Bing map for my region does not show any thing below this number. So there is not point allowing the user to zoom beyond this resolution. Your minimun map resolution may be higher or smaller, depending on the quality of your data.
2. Next, I determined what my optimun zoom resolution would be. Mine is 2.09612619755423. When the user clicks a point on the map or a row on the "FeatureDataGrid", I check if the current map resolution is less than my optimun resolution:
// XAML
<esri:FeatureDataGrid x:Name="MyDataGrid"SelectionChanged="MyDataGrid_SelectionChanged" />
// Event handler for FeatureDataGrid SelectionChanged
private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
// Retrieve current selected point from map or from FeatureDataGrid
IEnumerable<Graphic> myGraphics = MyDataGrid.SelectedGraphics;
Graphic graphic = myGraphics.Last<Graphic>();
// Create a point
MapPoint pt = new MapPoint(graphic.Geometry.Extent.XMin, graphic.Geometry.Extent.YMin);
// Set zoom resolution parameters
double zoomResolution = 2.09612619755423; // this is read-only property in my application
int mapResolution = Convert.ToInt32(MyMap.Resolution);
int zoomResolution = Convert.ToInt32(zoomResolution);
// Zoom to my optimun map resolution, if current map's resolution is higher
if (mapResolution > zoomResolution) MyMap.ZoomToResolution(ZoomResolution, pt);
// Is point inside envelope / map extent? It is, do not pan.
// The global field "currentEnvelope" is set at the map extent changed event:
// _currentEnvelope = e.NewExtent;
if (pt.Extent.Intersects(_currentEnvelope)) return;
/* The point falls outside the map viewable window.
* Pan to it preserving the current map zoom extent.
*/
Envelope env = new Envelope(pt.X - ZoomXRadius, pt.Y - ZoomYRadius, pt.X + ZoomXRadius, pt.Y + ZoomYRadius);
// Do pan to the point
MyMap.PanTo(env);
}
The properties ZoomXRadius and ZoomXRadius are read-only. These are set at the constructor. These hold the actual meter distance from the center of the optimun zoom resolution (envelope) of step 2. In my case, ZoomXRadius =1750 meters and ZoomXRadius = 950 meters.
In my case, I need to use "MyMap.PanTo" as well, for I want the map to display the selected point at the optimun zoom resolution.
Hope it helps.
Hugo.