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
BasemapStylesService
object. - Get the
BasemapStylesServiceInfo
object fromBasemapStylesService.info()
. - 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.worldview
: 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
// [WriteFile Name=CreateDynamicBasemapGallery, Category=Maps]
// [Legal]
// 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.
// [Legal]
#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD
#include "CreateDynamicBasemapGallery.h"
#include "Basemap.h"
#include "BasemapStyleInfo.h"
#include "BasemapStyleLanguageInfo.h"
#include "BasemapStyleListModel.h"
#include "BasemapStyleParameters.h"
#include "BasemapStylesService.h"
#include "BasemapStylesServiceInfo.h"
#include "Error.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Viewpoint.h"
#include "Worldview.h"
#include <QFuture>
using namespace Esri::ArcGISRuntime;
namespace
{
QMap<QString, BasemapStyleLanguageStrategy> languageStrategyNameToEnumMap{
{"Default", BasemapStyleLanguageStrategy::Default},
{"Global", BasemapStyleLanguageStrategy::Global},
{"Local", BasemapStyleLanguageStrategy::Local},
{"Application Locale", BasemapStyleLanguageStrategy::ApplicationLocale}
};
}
CreateDynamicBasemapGallery::CreateDynamicBasemapGallery(
QObject* parent /* = nullptr */)
: QObject(parent),
m_map(new Map(BasemapStyle::ArcGISNavigation, this)),
m_gallery(new BasemapStyleListModel(this))
{
BasemapStylesService* service = new BasemapStylesService(this);
connect(service, &BasemapStylesService::doneLoading, this, [this, service](const Error& /*error*/){
if (service->loadStatus() != LoadStatus::Loaded)
{
return;
}
m_styleInfos = service->info()->stylesInfo();
createGallery();
updateSelectedStyle("ArcGIS Navigation");
});
service->load();
}
CreateDynamicBasemapGallery::~CreateDynamicBasemapGallery()
{}
void CreateDynamicBasemapGallery::init()
{
// Register the map view for QML
qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
qmlRegisterType<CreateDynamicBasemapGallery>("Esri.Samples",
1,
0,
"CreateDynamicBasemapGallerySample");
}
// -------------------------------------------------- //
// Methods //
// -------------------------------------------------- //
void CreateDynamicBasemapGallery::createGallery()
{
m_gallery->insertItemsIntoGallery(m_styleInfos);
emit galleryChanged();
}
void CreateDynamicBasemapGallery::updateSelectedStyle(const QString& nameOfSelectedStyle)
{
const auto iteratorToInfoForSelectedStyle = std::find_if(m_styleInfos.begin(),
m_styleInfos.end(),
[nameOfSelectedStyle](const BasemapStyleInfo* info){
return info->styleName().compare(nameOfSelectedStyle, Qt::CaseInsensitive) == 0;
});
if (iteratorToInfoForSelectedStyle != m_styleInfos.end())
{
m_selectedStyle = *iteratorToInfoForSelectedStyle;
emit selectedStyleChanged();
updateLanguageStrategiesList();
updateLanguagesList();
updateWorldviewsList();
}
}
void CreateDynamicBasemapGallery::updateLanguageStrategiesList()
{
m_languageStrategies.clear();
if (m_selectedStyle->languageStrategies().isEmpty())
{
emit languageStrategiesChanged();
return;
}
m_languageStrategies.append("None"); // Add "None" to allow the user to unset this parameter in the UI.
for(const BasemapStyleLanguageStrategy& strategy : m_selectedStyle->languageStrategies())
{
QString displayName = languageStrategyNameToEnumMap.key(strategy);
if (displayName.isEmpty())
{
continue;
}
m_languageStrategies.append(displayName);
}
emit languageStrategiesChanged();
}
void CreateDynamicBasemapGallery::updateLanguagesList()
{
m_languages.clear();
if (m_selectedStyle->languages().isEmpty())
{
emit languagesChanged();
return;
}
m_languages.append("None"); // Add "None" to allow the user to unset this parameter in the UI.
for (const BasemapStyleLanguageInfo* info : m_selectedStyle->languages())
{
m_languages.append(info->displayName());
}
emit languagesChanged();
}
void CreateDynamicBasemapGallery::updateWorldviewsList()
{
m_worldviews.clear();
if (m_selectedStyle->worldviews().isEmpty())
{
emit worldviewsChanged();
return;
}
m_worldviews.append("None"); // Add "None" to allow the user to unset this parameter in the UI.
for(const Worldview* worldview : m_selectedStyle->worldviews())
{
m_worldviews.append(worldview->displayName());
}
emit worldviewsChanged();
}
void CreateDynamicBasemapGallery::loadBasemap(const QString& selectedStrategy,
const QString& selectedLanguage,
const QString& selectedWorldview)
{
if (!m_selectedStyle)
{
return;
}
BasemapStyleParameters* customisationParameters = new BasemapStyleParameters(this);
if (!selectedStrategy.isEmpty() && selectedStrategy != "None")
{
customisationParameters->setLanguageStrategy(languageStrategyNameToEnumMap[selectedStrategy]);
}
if (!selectedLanguage.isEmpty() && selectedLanguage != "None")
{
const QList<BasemapStyleLanguageInfo*> specificLanguages = m_selectedStyle->languages();
const auto iteratorToLanguageInfoForSelectedLanguage = std::find_if(specificLanguages.begin(),
specificLanguages.end(),
[selectedLanguage](const BasemapStyleLanguageInfo* info)
{
return info->displayName().compare(selectedLanguage, Qt::CaseInsensitive) == 0;
});
if (iteratorToLanguageInfoForSelectedLanguage != specificLanguages.end())
{
const BasemapStyleLanguageInfo* languageInfo = *iteratorToLanguageInfoForSelectedLanguage;
const QString code = languageInfo->languageCode();
customisationParameters->setSpecificLanguage(code);
}
}
if (!selectedWorldview.isEmpty() && selectedWorldview != "None")
{
const QList<Worldview*> worldviews = m_selectedStyle->worldviews();
const auto iteratorToSelectedWorldview = std::find_if(worldviews.begin(), worldviews.end(), [selectedWorldview](const Worldview* view)
{
return view->displayName().compare(selectedWorldview, Qt::CaseInsensitive) == 0;
});
if (iteratorToSelectedWorldview != worldviews.end())
{
customisationParameters->setWorldview(*iteratorToSelectedWorldview);
}
}
Basemap* newBasemap = new Basemap(m_selectedStyle->style(), customisationParameters, this);
const Viewpoint currentVewpoint = m_mapView->currentViewpoint(ViewpointType::CenterAndScale);
m_mapView->map()->setBasemap(newBasemap);
m_mapView->setViewpointAsync(currentVewpoint);
}
// -------------------------------------------------- //
// Property getters and setters //
// -------------------------------------------------- //
MapQuickView* CreateDynamicBasemapGallery::mapView() const
{
return m_mapView;
}
// Set the view (created in QML)
void CreateDynamicBasemapGallery::setMapView(MapQuickView* mapView)
{
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
m_mapView->setMap(m_map);
m_mapView->setViewpointAsync(Viewpoint{52.3433, -1.5796, 2500000});
emit mapViewChanged();
}
QAbstractListModel* CreateDynamicBasemapGallery::gallery() const
{
return m_gallery;
}
const QStringList& CreateDynamicBasemapGallery::languageStrategies() const
{
return m_languageStrategies;
}
const QStringList& CreateDynamicBasemapGallery::languages() const
{
return m_languages;
}
const QStringList& CreateDynamicBasemapGallery::worldviews() const
{
return m_worldviews;
}
int CreateDynamicBasemapGallery::indexOfSelectedStyle() const
{
return static_cast<int>(m_styleInfos.indexOf(m_selectedStyle));
}