Click or drag to resize
Code Example - MultipleMapsInMapView

Demonstrates switching out different Map objects (with different layers) for the MapView.Map Property.

Code Example
Multiple Maps In Map View

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="MultipleMapsInMapView.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/runtime/2013" 
        Title="MainWindow" Height="600" Width="800">

    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">

                <!-- Add a MapView Control.  We will set various Map's (with different Layers) in code behind based 
                on the users ListBox choice.  -->
                <esri:MapView x:Name="MyMapView" Width="637" Height="568" VerticalAlignment="Top"/>

                <TextBlock Width="10"/>
                <StackPanel Orientation="Vertical">
                    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Pick a Map" 
                               VerticalAlignment="Top" Height="20"/>

                    <!-- This is the ListBox that will have the various Map objects available. -->
                    <ListBox x:Name="ListBox1" VerticalAlignment="Top" Width="120" Height="300"
                             SelectionChanged="ListBox1_SelectionChanged_1"/>

                </StackPanel>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "MultipleMapsInMapView.MainWindow" to be just "MainWindow".

// INSTRUCTIONS:
// Click on the various Map names in the ListBox to switch between different Map controls as the MapView.Map Property.
// Each Map Control will have a different collection of layers (some with different Spatial References).

namespace MultipleMapsInMapView
{

    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add three strings for the different Map objects that will be renderer.
            ListBox1.Items.Add("Map1");
            ListBox1.Items.Add("Map2");
            ListBox1.Items.Add("Map3");
        }

        private void ListBox1_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {

            if (ListBox1.SelectedItem.ToString() == "Map1")
            {

                // Create an ArcGISTiledMapServiceLayer and an ArcGISDynamicMapServiceLayer and add it to Map1. Then set the MyMapView.Map to Map1. 

                Esri.ArcGISRuntime.Controls.Map Map1 = new Esri.ArcGISRuntime.Controls.Map();

                Esri.ArcGISRuntime.Layers.ArcGISTiledMapServiceLayer ArcGISTiledMapServiceLayer1 = new Esri.ArcGISRuntime.Layers.ArcGISTiledMapServiceLayer();
                ArcGISTiledMapServiceLayer1.ServiceUri = "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer";
                ArcGISTiledMapServiceLayer1.ID = "World_Light_Gray_Base";

                Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer ArcGISDynamicMapServiceLayer1 = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();
                ArcGISDynamicMapServiceLayer1.ServiceUri = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer";
                ArcGISDynamicMapServiceLayer1.ID = "USA_1990-2000_Population_Change";

                Map1.Layers.Add(ArcGISTiledMapServiceLayer1);
                Map1.Layers.Add(ArcGISDynamicMapServiceLayer1);

                MyMapView.Map = Map1;

            }
            else if (ListBox1.SelectedItem.ToString() == "Map2")
            {

                // Create an ArcGISImageServiceLayer and add it to Map2. Then set the MyMapView.Map to Map2. 

                Esri.ArcGISRuntime.Controls.Map Map2 = new Esri.ArcGISRuntime.Controls.Map();

                Esri.ArcGISRuntime.Layers.ArcGISImageServiceLayer ArcGISImageServiceLayer2 = new Esri.ArcGISRuntime.Layers.ArcGISImageServiceLayer();
                ArcGISImageServiceLayer2.ServiceUri = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/CaliforniaDEM/ImageServer";
                ArcGISImageServiceLayer2.ID = "CaliforniaDEM";

                Map2.Layers.Add(ArcGISImageServiceLayer2);

                MyMapView.Map = Map2;

            }
            else if (ListBox1.SelectedItem.ToString() == "Map3")
            {

                // Create an ArcGISTiledMapServiceLayer and a FeatureLayer and add it to Map3. Then set the MyMapView.Map to Map3. 

                Esri.ArcGISRuntime.Controls.Map Map3 = new Esri.ArcGISRuntime.Controls.Map();
                Esri.ArcGISRuntime.Layers.ArcGISTiledMapServiceLayer ArcGISTiledMapServiceLayer3 = new Esri.ArcGISRuntime.Layers.ArcGISTiledMapServiceLayer();
                ArcGISTiledMapServiceLayer3.ServiceUri = "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer";
                ArcGISTiledMapServiceLayer3.ID = "World_Light_Gray_Base";

                Esri.ArcGISRuntime.Layers.FeatureLayer FeatureLayer3 = new Esri.ArcGISRuntime.Layers.FeatureLayer();
                Esri.ArcGISRuntime.Data.ServiceFeatureTable ServiceFeatureTable3 = new Esri.ArcGISRuntime.Data.ServiceFeatureTable();
                ServiceFeatureTable3.ServiceUri = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer/0";
                ServiceFeatureTable3.OutFields = Esri.ArcGISRuntime.Tasks.Query.OutFields.All;
                FeatureLayer3.FeatureTable = ServiceFeatureTable3;

                Map3.Layers.Add(ArcGISTiledMapServiceLayer3);
                Map3.Layers.Add(FeatureLayer3);

                MyMapView.Map = Map3;

            }

        }

    }
}