Select to view content in your preferred language

Map.ZoomToResolution fails to change map

2321
10
10-22-2010 01:36 PM
BryanBaker
Regular Contributor
Map.ZoomToResolution is working the first time I call it, but on subsequent calls, the map does not change its extent at all. I suspect this has something to do with the fact that my map service's spatial reference uses WKT (well-known text) rather than a WKID. I have worked around this by creating my own method, where I calculate the zoom envelope manually based on the map size and desired resolution. Using that method works consistently.
0 Kudos
10 Replies
greatneal
New Contributor
Thanks bb1769, your codes are very useful for me.

my code

private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
 // Clear previous results
 GraphicsLayer graphicsLayer = mainMap.Layers["QueryGraphicsLayer"] as GraphicsLayer;
 graphicsLayer.ClearGraphics();

 // Check for new results 
 FeatureSet featureSet = args.FeatureSet;
 if (featureSet.Features.Count > 0)
 {
  // Add results to map
  foreach (Graphic resultFeature in featureSet.Features)
  {
   resultFeature.Symbol = QueryResultsMarkerSymbol;
   graphicsLayer.Graphics.Add(resultFeature);

   //zoom to some level if your layer is TiledMapServiceLayer
   TiledMapServiceLayer tiledLayer = mainMap.Layers["mainMapLayer"] as TiledMapServiceLayer;
   Lod lod = tiledLayer.TileInfo.Lods[5]; //zoom to 5 level
   double halfWidth = lod.Resolution * mainMap.ActualWidth / 2;
   double halfHeight = lod.Resolution * mainMap.ActualHeight / 2;
   
   ESRI.ArcGIS.Client.Geometry.MapPoint newMapPoint = (MapPoint)resultFeature.Geometry;
   Envelope newExtent = new Envelope(newMapPoint.X - halfWidth, newMapPoint.Y - halfHeight, newMapPoint.X + halfWidth, newMapPoint.Y + halfHeight);
   mainMap.ZoomTo(newExtent); //thanks for bb1769     
  }
 }
 else
 {
  MessageBox.Show("No features found");
 }
}
0 Kudos