ArcGIS API for Silverlight - Library Reference
ZoomTo Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > Map Class : ZoomTo Method

geom
Geometry to zoom to
Zooms the Map.Extent to a specified Geometry.

Syntax

Visual Basic (Declaration) 
Public Sub ZoomTo( _
   ByVal geom As Geometry _
) 
C# 
public void ZoomTo( 
   Geometry geom
)

Remarks

Geometry is an abstract base class for objects that define geometric shapes. The valid types of geometric objects that can be used for the ZoomIn Method as they have Extents that are useful for setting a Map.Extent include:

If the Geometry object parameter that is being passed into the Map.ZoomTo Method does not have the same SpatialReference as the Map.SpatialReference then the following runtime error message will result:

ArgumentException was unhandled by user code

Invalid spatial reference. Spatial reference must match map's spatial reference. Clear the map layers collection prior to changing the spatial reference.

Runtime ArgumentException error message when the Geometry.SpatialReference does not match the Map.SpatialReference

The example code in the Map.SpatialReference Property documentation contains one possible demonstration of how to change the Map.SpatialReference. You could use the same kind of reasoning to change the Geometry.SpatialReference to match the Map.SpatialReference.

Note: Using Methods are only available in code-behind. You cannot use a Method directly via XAML.

The following image shows a map with two layers. An ArcGISTiledMapServiceLayer (the World) is the first layer and an ArcGISDynamicMapServiceLayer (USA Highways) is the second layer. The top map in the image shows how by default the Map starts-up at the full extent of both layers. The bottom map in the image has as its start-up the second layer as the defined Map.Extent. To achieve the Map.Extent of the second layer as the start-up, the Map.ZoomTo Method in the second layers Initialized Event was used. The screen shots correspond to the code example in this document.

Demonstrating the difference between the default Map.Extent and defining a Map.Exent for a Layer.

Parameters

geom
Geometry to zoom to

Example

XAMLCopy Code
<!-- 
Add two layers with different extents. By default the Map starts up at the full extent of both layers.  
-->
            
<esri:Map Background="White" HorizontalAlignment="Left" Margin="705,69,0,0" 
          Name="Map3" VerticalAlignment="Top" Height="512" Width="1024">
            
  <esri:ArcGISTiledMapServiceLayer 
        Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
            
  <!--
  To make the Map's initial Extent start up as the Extent of the second layer (i.e. USA Highways)
  utilize the Initialized event handler.
  -->
  <esri:ArcGISDynamicMapServiceLayer 
        Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer" 
        Initialized="ArcGISDynamicMapServiceLayer_Initialized"/>
  
  <!--
  Note: It is not possible to perform the work of the Map1.ZoomTo Method directly in XAML.
  This must be done via code-behind (either C# or VB.NET). Adding the Initialized attribute
  to the XAML allows for use of code-behind.
  -->
</esri:Map>
C#Copy Code
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
  
  // Clear out any existing layers.
  Map1.Layers.Clear();
  
  // Add an ArcGISTiledMapsServiceLayer to the Map.
  ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = new ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer();
  myArcGISTiledMapServiceLayer.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer";
  Map1.Layers.Add(myArcGISTiledMapServiceLayer);
  
  // Add an ArcGISDynamicServiceLayer to the Map.
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer();
  myArcGISDynamicMapServiceLayer.Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer";
  Map1.Layers.Add(myArcGISDynamicMapServiceLayer);
  
  // By default the Map Control will set its initial extent to the full extent of both layers.
  
  // But if you want to have the initial extent of the Map Control be that of the second layer 
  // (the ArcGISDynamicServiceLayer) then the following line of code will cause that to happen.
  myArcGISDynamicMapServiceLayer.Initialized += new EventHandler<EventArgs>(ArcGISDynamicMapServiceLayer_Initialized);
  
}
  
private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e)
{
  
  // The ArcGISDynamicMapServiceLayer.IntialExtent, and Layer.FullExtent can produce different results.
  // Uncomment/comment the two options to suit your preference:
  
  // Option #1: ArcGISDynamicMapServiceLayer.InitialExtent
  if (sender is ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  {
      ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)sender;
      Map1.ZoomTo(myArcGISDynamicMapServiceLayer.InitialExtent);
  }
  
  //// Option #2: Layer.FullExtent
  //if (sender is ESRI.ArcGIS.Client.Layer)
  //{
  //    ESRI.ArcGIS.Client.Layer myLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)sender;
  //    Map1.ZoomTo(myLayer.FullExtent);
  //}
  
}
VB.NETCopy Code
Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
  
  ' Clear out any existing layers.
  Map1.Layers.Clear()
  
  ' Add an ArcGISTiledMapsServiceLayer to the Map.
  Dim myArcGISTiledMapServiceLayer As New ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer
  myArcGISTiledMapServiceLayer.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"
  Map1.Layers.Add(myArcGISTiledMapServiceLayer)
  
  ' Add an ArcGISDynamicServiceLayer to the Map.
  Dim myArcGISDynamicMapServiceLayer As New ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer
  myArcGISDynamicMapServiceLayer.Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer"
  Map1.Layers.Add(myArcGISDynamicMapServiceLayer)
  
  ' By default the Map Control will set its initial extent to the full extent of both layers.
  
  ' But if you want to have the initial extent of the Map Control be that of the second layer 
  ' (the ArcGISDynamicServiceLayer) then the following line of code will cause that to happen.
  AddHandler myArcGISDynamicMapServiceLayer.Initialized, AddressOf ArcGISDynamicMapServiceLayer_Initialized
  
  End Sub
  
  Private Sub ArcGISDynamicMapServiceLayer_Initialized(ByVal sender As Object, ByVal e As EventArgs)
  
  ' The ArcGISDynamicMapServiceLayer.IntialExtent, and Layer.FullExtent can produce different results.
  ' Uncomment/comment the two options to suit your preference:
  
  ' Option #1: ArcGISDynamicMapServiceLayer.InitialExtent
  If TypeOf sender Is ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer Then
      Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = sender
      Map1.ZoomTo(myArcGISDynamicMapServiceLayer.InitialExtent)
  End If
  
  '' Option #2: Layer.FullExtent
  'If TypeOf sender Is ESRI.ArcGIS.Client.Layer Then
  '    Dim myLayer As ESRI.ArcGIS.Client.Layer = sender
  '    Map1.ZoomTo(myLayer.FullExtent)
  'End If
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.