Creating a map
Using the ArcGIS API for Windows Phone, a map can be added to any Windows Phone application. To add a map, you need to include the following in your code:
- A reference to the ESRI.ArcGIS.Client namespace.
- .NET code or XAML markup including the Map control.
The following code example shows the XAML markup for the ContentPanel used to include the ArcGIS API for Windows Phone Map control in a simple Windows Phone application. The Map in this example contains a single ArcGIS tiled map service layer. In addition, a custom startup extent has been added to the Map control to define the map extent displayed when the application starts.
<Grid x:Name="ContentPanel" Grid.Row="1">
<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:ArcGISTiledMapServiceLayer>
</esri:Map.Layers>
</esri:Map>
</Grid>
The remainder of this topic walks you through the steps to include this XAML in your Windows Phone application.
Creating the map
The following steps assume you have created a Windows Phone application in Visual Studio and are working in the XAML view of the main page of your application (for example, MainPage.xaml).
- Add a reference to ESRI.ArcGIS.Client.dll in your application.
- Add a namespace reference to the ESRI.ArcGIS.Client library and namespace by inserting the attribute shown in the following code as an attribute of the phone:PhoneApplicationPage element in the XAML.
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
This enables the use of classes and components 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 assembly. In this case the identifier is esri.
- Add the Map control element to the ContentPanel in the page. 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="ContentPanel" Grid.Row="1"> <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 (e.g. 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. Tip:
Specifying the Layers property inside the Map tag is optional. You can instead include the esri:ArcGISTiledMapServiceLayer tag directly inside the esri:Map tag.
<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:ArcGISTiledMapServiceLayer> </esri:Map.Layers> </esri:Map>
- Define a startup extent for the map. Use attribute syntax to define the Extent property on the Map control. The attribute value is a comma-delimited set of four numbers specifying the minimum x, minimum y, maximum x, and maximum y values, respectively.
<esri:Map x:Name="MyMap" Extent="-120, 20, -100, 40">
- Compile and run the application. You'll see an ArcGIS Windows Phone Map control centered on the western United States in the content of the Windows Phone application.
Initial map extent and properties
The following properties can be defined for an ArcGIS Windows Phone Map control upon initialization:
- Initial extent
- Spatial reference
- Minimum and maximum resolution
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 tile 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 (for example, an ArcGISTiledMapServiceLayer) 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 map's 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">
<!-- additional Map properties and settings go here -->
</esri:Map>
Setting the 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>
<!-- additional Map properties and settings go here -->
</esri:Map>
Setting the extent and spatial reference using code-behind
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, where the Map control has an x:Name of MyMap. 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.dll to use the following code snippet in your application.
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 SpatialReference(102100);
MyMap.Extent = initialExtent;