dot
Creating a map

When adding a Map control to your Silverlight application, your code will always contain two parts:

The following example shows the XAML markup in a simple Silverlight application that includes an ArcGIS Silverlight API Map control which contains a single ArcGIS Server tiled map service layer. In addition, a custom startup extent has been added to the map control to define the the initial map extent when the application starts. The remainder of this document will walk you through the steps to include this XAML in your Silverlight application.

<UserControl x:Class="SilverlightApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client">
<Grid x:Name="LayoutRoot" Background="White">

<esri:Map x:Name="MyMap" Extent="-120, 20, -100, 40" >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
</esri:Map.Layers>
</esri:Map>

</Grid>
</UserControl>

How to create a map

The following steps assume you have created a Silverlight application in Visual Studio and are working in the XAML view of the main page (e.g. Page.xaml) of your application.

  1. Add a reference to the ESRI.ArcGIS.Client.dll in your application.

  2. In XAML, add an XML namespace that references the ESRI.ArcGIS.Client namespace in the ESRI.ArcGIS.Client assembly. The value after the XML namespace attribute "xmlns" defines the namespace identifier you'll use to reference controls in the ESRI.ArcGIS.Client namespace. In this case the identifier is "esri".

    <UserControl x:Class="SilverlightApp.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client">

  3. Optionally, remove the Width and Height attributes in the user control that defines the page. This will allow the page contents to fill the browser window.

  4. Add the Map control element to a container element in the page. In this example the container is a Grid. Use the ESRI.ArcGIS.Client namespace identifier "esri" to define the namespace that contains the Map control. Give the Map control a unique name using the "x:Name" attribute.

    <Grid x:Name="LayoutRoot" Background="White">        
    <esri:Map x:Name="MyMap" >
    </esri:Map>

    </Grid>
  5. Add an ArcGIS Server tiled map service layer to the map. The map contains a collection of layers referenced by the Layers property. In XAML you can modify the contents of the Layers property using property element syntax. Basically this means you can specify the property name as an child element (e.g. Map.Layers) and include the appropriate contents. In this case the appropriate content is a ArcGISTileMapServiceLayer element which will enable you to reference an ArcGIS Server cached map service. Define the Url to the map service endpoint and include a unique id for the layer.

    <esri:Map x:Name="MyMap" >
    <esri:Map.Layers>
    <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
    Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" /> </esri:Map.Layers>
    </esri:Map>

  6. Define a startup extent for the map. Use attribute syntax to define the Extent property on the map. The attribute value is a comma-delimited set of four numbers specifying the min x, min y, max x, and max y values.

    <esri:Map x:Name="MyMap" Extent="-120, 20, -100, 40" >    
  7. Compile and run your application. You should see an ArcGIS Silverlight Map control centered on the western United States fill the browser window.

How to define a spatial reference for the Map

By default, the first layer with a valid spatial reference defines the spatial reference for the map. Dynamic ArcGIS Server map and image services as well as feature layers (FeatureLayer) will be reprojected to the map's spatial reference, if necessary. Tiled map services layers will not be reprojected - the spatial reference of the layer and map must match for the layer to display in the map. To define an explicit spatial reference for the map, create an Envelope and assign a SpatialReference. Use the following XAML as a guide:

<esri:Map x:Name="MyMap">
<esri:Map.Extent>
<esriGeometry:Envelope XMin="661140" YMin="-1420246" XMax="3015668" YMax="1594451" >
<esriGeometry:Envelope.SpatialReference>
<esriGeometry:SpatialReference WKID="26777"/>
</esriGeometry:Envelope.SpatialReference>
</esriGeometry:Envelope>
</esri:Map.Extent>

. . .