Create dynamic basemap gallery

View on GitHubSample viewer app

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

screenshot

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 from BasemapStylesService.info().
  • 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.
    • worldview: 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.cppCreateDynamicBasemapGallery.cppCreateDynamicBasemapGallery.hBasemapStyleListModel.hBasemapStyleListModel.cppCreateDynamicBasemapGallery.qml
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// [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

// sample headers
#include "BasemapStyleListModel.h"
#include "CreateDynamicBasemapGallery.h"

// ArcGIS Maps SDK headers
#include "Basemap.h"
#include "BasemapStyleInfo.h"
#include "BasemapStyleLanguageInfo.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"

// Qt headers
#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));
}

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