Create dynamic basemap gallery

View inMAUIWPFWinUIView on GitHubSample viewer app

Implement a basemap gallery that automatically retrieves the latest customization options from the basemap styles service.

CreateDynamicBasemapGallery

Use case

Multi-use and/or international applications benefit from the ability to change a basemap's style or localize the basemap. For example, an application used for ecological surveys might include navigation functionality to guide an ecologist to a location and functionality for inputting data. When traveling, a user is likely to benefit from a map with a style that emphasizes the transport infrastructure (e.g. ArcGIS Navigation). However, during surveys a user is likely to benefit from a map with a style that highlights features in the terrain (e.g. ArcGIS Terrain). Implementing a basemap gallery with customization options in an application gives a user the freedom to select a basemap with a style and features (e.g. language of labels) suitable for the task they are undertaking. Making the basemap gallery dynamic ensures the latest customization options are automatically included.

How to use the sample

When launched, this sample displays a map containing a button that, when pressed, displays a gallery of all styles available in the basemap styles service. Selecting a style results in the drop-down menus at the base of the gallery becoming enabled or disabled. A disabled menu indicates that the customization cannot be applied to the selected style. Once a style and any desired customizations have been selected, pressing Load will update the basemap in the map view.

How it works

  • Instantiate and load a BasemapStylesServiceInfo object.
  • Access the list of BasemapStyleInfo objects using BasemapStylesServiceInfo.StylesInfo. These BasemapStyleInfo objects contain up-to-date information about each of the styles supported by the Maps SDK, including:
    • StyleName: The human-readable name of the style.
    • Style: The BasemapStyle enumeration value representing this style in the Maps SDK.
    • Thumbnail: An image that can be used to display a preview of the style.
    • Languages: A list of BasemapStyleLanguageInfo objects, which provide information about each of the specific languages that can be used to customize labels on the style.
    • Worldviews: A list of Worldview objects, which provide information about each representation of a disputed boundary that can be used to customize boundaries on the style.
  • The information contained in the list of BasemapStyleInfo objects can be used as the data model for a basemap gallery UI component.

Relevant API

  • BasemapStyleInfo
  • BasemapStyleLanguageInfo
  • BasemapStyleParameters
  • BasemapStylesService
  • BasemapStylesServiceInfo
  • Worldview

Additional information

This sample demonstrates how to implement a basemap gallery using the Maps SDK. The styles and associated customization options used for the gallery are retrieved from the basemap styles service. A ready-made basemap gallery component is also available in the toolkit's provided with each SDK. To see how the ready-made basemap gallery toolkit component can be integrated into a Maps SDK application refer to the Set Basemap sample.

Tags

basemap, languages, service, style

Sample Code

CreateDynamicBasemapGallery.xaml.csCreateDynamicBasemapGallery.xaml.csCreateDynamicBasemapGallery.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2024 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Mapping;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGIS.WPF.Samples.CreateDynamicBasemapGallery
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Create dynamic basemap gallery",
        category: "Map",
        description: "Implement a basemap gallery that automatically retrieves the latest customization options from the basemap styles service.",
        instructions: "When launched, this sample displays a map containing a button that, when pressed, displays a gallery of all styles available in the basemap styles service. Selecting a style results in the drop-down menus at the base of the gallery becoming enabled or disabled. A disabled menu indicates that the customization cannot be applied to the selected style. Once a style and any desired customizations have been selected, pressing `Load` will update the basemap in the map view.",
        tags: new[] { "basemap", "languages", "service", "style" })]
    [ArcGIS.Samples.Shared.Attributes.OfflineData()]
    public partial class CreateDynamicBasemapGallery
    {
        public CreateDynamicBasemapGallery()
        {
            InitializeComponent();
            _ = Initialize();
        }

        private async Task Initialize()
        {
            // Create a new map with the ArcGIS Navigation basemap style.
            MyMapView.Map = new Map(BasemapStyle.ArcGISNavigation);

            // Create a new basemap styles service, pulling in the available styles and their information.
            BasemapStylesServiceInfo service = await BasemapStylesServiceInfo.CreateAsync();

            // Populate the basemap style gallery.
            BasemapStyleGallery.ItemsSource = service.StylesInfo;

            // Listen for basemap style selection events.
            BasemapStyleGallery.SelectionChanged += BasemapStyleGallery_SelectionChanged;
        }

        private void BasemapStyleGallery_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Get the selected basemap style info.
            var styleInfo = (BasemapStyleInfo)BasemapStyleGallery.SelectedItem;

            // Set the pickers to the available options for the selected basemap.
            StrategyPicker.ItemsSource = styleInfo.LanguageStrategies;
            LanguagePicker.ItemsSource = styleInfo.Languages;
            WorldviewPicker.ItemsSource = styleInfo.Worldviews;

            // Disable any pickers that have no items.
            LanguagePicker.IsEnabled = LanguagePicker.Items.Count > 0;
            WorldviewPicker.IsEnabled = WorldviewPicker.Items.Count > 0;
            StrategyPicker.IsEnabled = StrategyPicker.Items.Count > 0;
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            // Return if no basemap style is selected.
            if (BasemapStyleGallery.SelectedItem == null) return;

            // Create a new basemap style parameters object.
            var basemapStyleParameters = new BasemapStyleParameters();

            // Set the language to the selected language.
            if (LanguagePicker.SelectedItem != null)
                basemapStyleParameters.SpecificLanguage = (LanguagePicker.SelectedItem as BasemapStyleLanguageInfo).CultureInfo;

            // Set the worldview to the selected worldview.
            if (WorldviewPicker.SelectedItem != null)
                basemapStyleParameters.Worldview = (WorldviewPicker.SelectedItem as Worldview);

            // Set the strategy to the selected strategy.
            if (StrategyPicker.SelectedItem != null)
                basemapStyleParameters.LanguageStrategy = (BasemapStyleLanguageStrategy)StrategyPicker.SelectedItem;

            // Determine the basemap style of the currently selected basemap.
            BasemapStyle selectedBasemapStyle = ((BasemapStyleInfo)BasemapStyleGallery.SelectedItem).Style;

            // Update the map's basemap.
            MyMapView.Map.Basemap = new Basemap(selectedBasemapStyle, basemapStyleParameters);
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.