Click or drag to resize
Code Example - BaseMaps

Demonstrates switching out various BaseMap (via ArcGISTiledMapServiceLayer) layers served up by ArcGIS Online.

Code Example
Base Maps

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="BaseMaps.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 x:Name="LayoutRoot">

        <StackPanel Orientation="Vertical">

            <!-- Add a TextBlock to hold the instructions on how to use the sample code. -->
            <TextBlock Name="TextBlock1" TextWrapping="Wrap" Height="77" />

            <StackPanel Orientation="Horizontal">

                <!-- Add a ComboBox to hold Urls to BaseMaps on ArcGIS Online. -->
                <ComboBox Name="ComboxBox1" Width="600" Margin="0,0,10,0" />

                <!-- Add a Button to switch the BaseMap layer (an ArcGISTileMapServiceLayer) in the Map Control. -->
                <Button Content="Change BaseMap" Name="Button1" Width="160" Click="Button1_Click"/>

            </StackPanel>

            <!-- Add a MapView Control. -->
            <esri:MapView x:Name="MapView1" Background="Blue" Margin="0,10,0,0" Height="458" >

                <!-- Add a Map. -->
                <esri:Map x:Name="Map1" />

            </esri:MapView>

        </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 "BaseMaps.MainWindow" to be just "MainWindow".

namespace BaseMaps
{

    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            // This call is required by the designer.
            InitializeComponent();

            // Provide instructions on how to use the sample. 
            TextBlock1.Text = "When the application loads, choose a Url from the ComboBox drop-down and click the 'Change BaseMap' button. An " + 
                "ArcGISTiledMapServiceLayer will load and be displayed in the Map. The listing of Urls available come from BaseMaps available " + 
                "on ArcGIS Online (http://www.arcgis.com/features/maps/basemaps.html). Additional BaseMaps are periodically added so check back " + 
                "often for additional layer that may be available.";

            // Add the URL's for the various BaseMaps available on ArcGIS Online (http://www.arcgis.com/features/maps/basemaps.html).
            ComboxBox1.Items.Add("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"); // World Topographic Map
            ComboxBox1.Items.Add("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"); // World Street Map
            ComboxBox1.Items.Add("http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"); // Light Gray Canvas
            ComboxBox1.Items.Add("http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer"); // National Geographic World Map
            ComboxBox1.Items.Add("http://services.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer"); // Ocean Basemap
            ComboxBox1.Items.Add("http://services.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer"); // Terrain with Labels
            ComboxBox1.Items.Add("http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"); // World Imagery
            ComboxBox1.Items.Add("http://tiles.arcgis.com/tiles/IEuSomXfi6iB7a25/arcgis/rest/services/World_Globe_1790/MapServer"); // World Globe 1790
            ComboxBox1.Items.Add("http://tiles.arcgis.com/tiles/IEuSomXfi6iB7a25/arcgis/rest/services/World_Globe_1812/MapServer"); // World Globe 1812
        }

        private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Clear out any existing layers in the Map.
            MapView1.Map.Layers.Clear();

            // Get the Url string for the various ArcGISTiledMapServiceLayer BaseMap's on ArcGIS Online.
            string myUserChoice = (string)ComboxBox1.SelectedValue;

            // Create a new ArcGISTiledMapServiceLayer and set its .ServiceUri Property.
            Esri.ArcGISRuntime.Layers.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISTiledMapServiceLayer();
            myArcGISTiledMapServiceLayer.ServiceUri = myUserChoice;

            // Add the ArcGISTiledMapServiceLayer to the LayercCollection of the Map.
            MapView1.Map.Layers.Add(myArcGISTiledMapServiceLayer);
        } 
    }
}