Creating a map
When adding a Map control to your ArcGIS API for WPF application, your code will always contain the following:
- A reference to the ArcGIS schema
- .NET code or XAML markup including the Map control
The following code example shows the XAML markup in a simple WPF application that includes an ArcGIS API for WPF Map control containing a single ArcGIS Server tiled map service layer. In addition, a custom startup extent has been added to the Map control to define the initial map extent when the application starts. The remainder of this topic walks you through the steps to include this XAML in your WPF application.
<Window x:Class="SimpleMap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
Title="MainWindow" Height="350" Width="525">
<Grid>
<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>
</Window>
Creating the map
The following steps assume you have created a WPF application in Visual Studio and are working in the XAML view of the main window of your application (for example, MainWindow.xaml).
- Add a reference to ESRI.ArcGIS.Client.dll in your application.
- In XAML, add an XML namespace that references the ArcGIS schema
for WPF. This schema enables the use of classes and
components in the ESRI.ArcGIS.Client, ESRI.ArcGIS.Client.Behaviors,
and ESRI.ArcGIS.Client.Toolkit assemblies. The value after the XML
namespace attribute "xmlns" defines the namespace identifier you'll
use to reference controls in these assemblies. In this case the
identifier is "esri".
<Window x:Class="WPFApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:esri="http://schemas.esri.com/arcgis/client/2009" Title="MainWindow" Height="350" Width="525">
- Add the Map control element to a container element in the window. 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> <esri:Map x:Name="MyMap" > </esri:Map> </Grid>
- 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. This means you can specify the property name as a child element (for example, Map.Layers) and include the appropriate contents. In this case, the appropriate content is an ArcGISTiledMapServiceLayer element that 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. Note:
Specifying the Layers property inside the Map tag is optional.
<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>
- 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" >
- Compile and run the application. You'll see an ArcGIS API for WPF Map control centered on the western United States fill the window.
Initial map extent and properties
The following properties can be defined for an ArcGIS API for WPF Map control upon initialization:
- Initial extent
- Spatial reference
- Minimum and maximum resolution (lowest level of detail in the first tiled map service layer)
By default, the initial extent is the full extent of all layers in the map. The spatial reference is defined by the first layer with a spatial reference in the layer collection. Layers in the map must support the spatial reference defined by the map. If the spatial reference does not match, the layer (that is, service) must be capable of reprojecting its content to be displayed correctly in the map. If the spatial reference of a tiled layer (for example, an ArcGISTiledMapServiceLayer) does not match the map, it will not be displayed.
The spatial reference of the map cannot be changed once it has been set.
If a map contains any tile layers, the minimum and maximum resolutions are defined by the collection of tile layers.
Each of these properties can be explicitly defined when the map initializes.
Setting the startup extent
Use the Extent property on the Map in XAML to define the comma-delimited string defining the minimum X, minimum Y, maximum X, and maximum Y.
<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>
Defining 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>
<esri:Envelope XMin="661140" YMin="-1420246" XMax="3015668" YMax="1594451" >
<esri:Envelope.SpatialReference>
<esri:SpatialReference WKID="26777"/>
</esri:Envelope.SpatialReference>
</esri:Envelope>
</esri:Map.Extent>
You can also define the initial map extent and spatial reference in code-behind where you can use transformation logic to convert coordinates from one well-known spatial reference to another. The following code can be added to the constructor of the container of the Map control. It takes an envelope defined in decimal degrees and converts it to Web Mercator (WKID: 102100), then applies the envelope as an extent on the map.
You'll need to add a reference to ESRI.ArcGIS.Client.Bing to use the following code snippet.
ESRI.ArcGIS.Client.Geometry.Envelope initialExtent = new
ESRI.ArcGIS.Client.Geometry.Envelope(ESRI.ArcGIS.Client.Bing.Transform.GeographicToWebMercator( new
ESRI.ArcGIS.Client.Geometry.MapPoint(-130, 20)),
ESRI.ArcGIS.Client.Bing.Transform.GeographicToWebMercator(new ESRI.ArcGIS.Client.Geometry.MapPoint(-65, 55)));
initialExtent.SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100);
MyMap.Extent = initialExtent;