ArcGIS Dynamic Map Service Layer

Download Samples Repository

Description

Demonstrates adding an ArcGIS dynamic map service layer to a Map in XAML.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ArcGISDynamicMapServiceLayerSample"
             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/runtime/2013">
    <Grid Background="LightBlue">
		<esri:MapView x:Name="MyMapView">
            <esri:Map InitialViewpoint="-14675766.357,2695407.734,-6733121.861,6583994.101,102100">
                <esri:ArcGISDynamicMapServiceLayer ID="USA"
                    ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer" />
            </esri:Map>
        </esri:MapView>

        <Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top" 
				Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
			<ItemsControl x:Name="visibleLayers">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Item1, Mode=OneWay}" Tag="{Binding}" IsChecked="{Binding Item3, Mode=OneWay}" Click="LayerCheckBox_Click" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Border>
    </Grid>
</UserControl>

using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Demonstrates adding an ArcGIS dynamic map service layer to a Map in XAML. 
    /// </summary>
    /// <title>ArcGIS Dynamic Map Service Layer</title>
	/// <category>Layers</category>
	/// <subcategory>Dynamic Service Layers</subcategory>
	public partial class ArcGISDynamicMapServiceLayerSample : UserControl
    {
        private ArcGISDynamicMapServiceLayer _usaLayer;

		public ArcGISDynamicMapServiceLayerSample()
		{
			InitializeComponent();

			MyMapView.Map.SpatialReference = SpatialReferences.WebMercator;
			_usaLayer = MyMapView.Map.Layers["USA"] as ArcGISDynamicMapServiceLayer;
			MyMapView.LayerLoaded += MyMapView_LayerLoaded;
		}

		private void MyMapView_LayerLoaded(object sender, LayerLoadedEventArgs e)
		{
			if (e.LoadError != null)
			{
				MessageBox.Show(e.LoadError.Message, "Layer Error");
				return;
			}

			if (e.Layer == _usaLayer)
			{

				_usaLayer.VisibleLayers = new ObservableCollection<int>() { 0, 1, 2, 3 };

				visibleLayers.ItemsSource = _usaLayer.ServiceInfo.Layers
					.Select((info, idx) => new Tuple<string, int, bool>(info.Name, idx, info.DefaultVisibility));
			}
		}

		private void LayerCheckBox_Click(object sender, RoutedEventArgs e)
		{
			var checkBox = e.OriginalSource as CheckBox;
			if (checkBox != null)
			{
				int layerIndex = ((Tuple<string, int, bool>)checkBox.Tag).Item2;

				if (checkBox.IsChecked == true)
					_usaLayer.VisibleLayers.Add(layerIndex);
				else
					_usaLayer.VisibleLayers.Remove(layerIndex);
			}
		}
	}
}
Feedback on this topic?