Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Basemaps

Download Samples Repository

Description

This sample demonstrates how to search for, return, and dynamically use basemaps from ArcGIS Online.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.PortalBasemaps"
             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 x:Name="layoutGrid">
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="*" />
			<ColumnDefinition Width="Auto" />
		</Grid.ColumnDefinitions>
        <esri:MapView x:Name="MyMapView" />

        <ProgressBar x:Name="progress" IsIndeterminate="True" Margin="0,0,0,20"
                     HorizontalAlignment="Center" VerticalAlignment="Bottom" Width="300"
                     Visibility="Collapsed" />
		<ScrollViewer Grid.Column="1"  MaxWidth="200" HorizontalAlignment="Left" Margin="5">
			<ItemsControl x:Name="basemapList">
				<ItemsControl.ItemTemplate>
					<DataTemplate>
						<Button Click="BaseMapButton_Click" Margin="5">
							<StackPanel>
								<Image Source="{Binding ThumbnailUri}" Margin="3" Width="200" Height="133" />
								<TextBlock Text="{Binding Title}" />
							</StackPanel>
						</Button>
					</DataTemplate>
				</ItemsControl.ItemTemplate>
			</ItemsControl>
		</ScrollViewer>
	</Grid>
</UserControl>
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.WebMap;
using System;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates how to search for, return, and dynamically use basemaps from ArcGIS Online.
    /// </summary>
    /// <title>Basemaps</title>
    /// <category>Portal</category>
    public partial class PortalBasemaps : UserControl
    {
        private WebMapViewModel _currentVM;

        /// <summary>Construct Portal Basemaps sample control</summary>
        public PortalBasemaps()
        {
            InitializeComponent();

            MyMapView.Loaded += MyMapView_Loaded;
        }

        // Initialize the display with a web map and search portal for basemaps
        private async void MyMapView_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                progress.Visibility = Visibility.Visible;

                // Load initial webmap
                var portal = await ArcGISPortal.CreateAsync();
                var item = await ArcGISPortalItem.CreateAsync(portal, "3679c136c2694d0b95bb5e6c3f2b480e");
                var webmap = await WebMap.FromPortalItemAsync(item);
                _currentVM = await WebMapViewModel.LoadAsync(webmap, portal);
                MyMapView.Map = _currentVM.Map;

                // Load portal basemaps
                var result = await portal.ArcGISPortalInfo.SearchBasemapGalleryAsync();
                basemapList.ItemsSource = result.Results;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sample Error");
            }
            finally
            {
                progress.Visibility = Visibility.Collapsed;
            }
        }

        // Switch current maps basemap when the user selects a portal item
        private async void BaseMapButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                progress.Visibility = Visibility.Visible;

                var item = ((Button)sender).DataContext as ArcGISPortalItem;
                var webmap = await WebMap.FromPortalItemAsync(item);
                var basemapVM = await WebMapViewModel.LoadAsync(webmap, _currentVM.ArcGISPortal);
				_currentVM.Basemap = basemapVM.Basemap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sample Error");
            }
            finally
            {
                progress.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Feedback on this topic?