Load WebMap

Download Samples Repository

Description

This sample demonstrates adding data a WebMap from ArcGIS Online to an application.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.LoadWebMap"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid x:Name="layoutGrid">
        <esri:MapView x:Name="MyMapView" />

		<Border x:Name="uiPanel" 
				Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20" Width="375">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="Select a featured map or enter the ID of a WebMap to load:" 
						   Margin="0,0,0,2" TextWrapping="Wrap" />
                <ComboBox x:Name="comboWebMap" IsEditable="True" 
						  DisplayMemberPath="Title" SelectedValuePath="Id" />
                <Button Content="Load WebMap" IsDefault="True" 
						HorizontalAlignment="Center" Margin="0,8,0,0" 
						Click="LoadButton_Click" />

                <ProgressBar x:Name="progress" IsIndeterminate="True"
							 Margin="12,12,12,0" Visibility="Hidden" />

                <Grid x:Name="detailsPanel" Margin="0,12,0,0" Visibility="Collapsed">
                    <Grid.Resources>
                        <Style TargetType="TextBlock">
                            <Setter Property="Margin" Value="4,2" />
                        </Style>
                    </Grid.Resources>
                    
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    
                    <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" 
							   Text="WebMap Details" FontSize="14" FontWeight="Bold" />

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Title:" FontStyle="Italic" />
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Title}" TextWrapping="Wrap" />

                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Snippet:" FontStyle="Italic" />
                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Snippet}" TextWrapping="Wrap" />

                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Owner:" FontStyle="Italic" />
                    <TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Owner}" TextWrapping="Wrap" />
                </Grid>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.WebMap;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates adding data a WebMap from ArcGIS Online to an application.
    /// </summary>
    /// <title>Load WebMap</title>
    /// <category>Portal</category>
    public partial class LoadWebMap : UserControl
    {
        private ArcGISPortal _portal;

        /// <summary>Construct Load WebMap sample control</summary>
        public LoadWebMap()
        {
            InitializeComponent();

            Loaded += LoadWebMap_Loaded;
        }

        // Loads UI elements and an initial webmap
        private async void LoadWebMap_Loaded(object sender, RoutedEventArgs e)
        {
            _portal = await ArcGISPortal.CreateAsync();

            var searchParams = new SearchParameters("type: \"web map\" NOT \"web mapping application\"");
            var result = await _portal.ArcGISPortalInfo.SearchHomePageFeaturedContentAsync(searchParams);
            comboWebMap.ItemsSource = result.Results;

            var webmap = result.Results.FirstOrDefault();
            if (webmap != null)
            {
                comboWebMap.SelectedIndex = 0;
                await LoadWebMapAsync(webmap.Id);
            }
        }

        // Loads a webmap on load button click
        private async void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            string id = string.Empty;
            if (comboWebMap.SelectedIndex >= 0)
                id = comboWebMap.SelectedValue as string;
            else
                id = comboWebMap.Text;

            await LoadWebMapAsync(id);
        }

        // Loads the given webmap
        private async Task LoadWebMapAsync(string wmId)
        {
            try
            {
                progress.Visibility = Visibility.Visible;

                var item = await ArcGISPortalItem.CreateAsync(_portal, wmId);
                var webmap = await WebMap.FromPortalItemAsync(item);
                var vm = await WebMapViewModel.LoadAsync(webmap, _portal);
                MyMapView.Map = vm.Map;

                detailsPanel.DataContext = item;
                detailsPanel.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                detailsPanel.Visibility = Visibility.Visible;
                MessageBox.Show(ex.Message, "Sample Error");
            }
            finally
            {
                progress.Visibility = Visibility.Hidden;
            }
        }
    }
}
Feedback on this topic?