Implement a basemap gallery that automatically retrieves the latest customization options from the basemap styles service.
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 usingBasemapStylesServiceInfo.StylesInfo
. TheseBasemapStyleInfo
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
: TheBasemapStyle
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 ofBasemapStyleLanguageInfo
objects, which provide information about each of the specific languages that can be used to customize labels on the style.Worldviews
: A list ofWorldview
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
// 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;
namespace ArcGIS.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.ToList();
LanguagePicker.ItemsSource = styleInfo.Languages.ToList();
WorldviewPicker.ItemsSource = styleInfo.Worldviews.ToList();
// Disable any pickers that have no items.
LanguagePicker.IsEnabled = styleInfo.Languages.Any();
WorldviewPicker.IsEnabled = styleInfo.Worldviews.Any();
StrategyPicker.IsEnabled = styleInfo.LanguageStrategies.Any();
}
private void LoadButton_Click(object sender, EventArgs 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);
// Hide the gallery.
HideGallery();
}
private void ShowGallery_Clicked(object sender, EventArgs e)
{
DynamicBasemapStyleGallery.IsVisible = true;
TransparentBackground.IsVisible = true;
ShowGalleryButton.IsEnabled = false;
}
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
HideGallery();
}
private void HideGallery()
{
DynamicBasemapStyleGallery.IsVisible = false;
TransparentBackground.IsVisible = false;
ShowGalleryButton.IsEnabled = true;
}
}
}