Skip to content
View on GitHubSample viewer app

Load ArcGIS vector tiled layers using custom styles.

screenshot

Use case

Vector tile basemaps can be created in ArcGIS Pro and published as offline packages or online services. You can create a custom style tailored to your needs and easily apply them to your map. ArcGISVectorTiledLayer has many advantages over traditional raster based basemaps (ArcGISTiledLayer), including smooth scaling between different screen DPIs, smaller package sizes, and the ability to rotate symbols and labels dynamically.

How to use the sample

Pan and zoom to explore the vector tile basemap. Select a theme to see it applied to the vector tile basemap.

How it works

  1. Create a PortalItem for each vector tiled layer.
  2. Create a Map and set the default Viewpoint.
  3. Export the light and dark offline custom styles.
    i. Create a ExportVectorTilesTask using the portal item.
    ii. Get the path for where the cache is being stored locally.
    iii. Return with the cache if the path already exists.
    iv. Else, create a ExportVectorTilesJob by having the task call ExportStyleResourceCache with the path as a parameter.
    v. Start the job.
    vi. When the job completes, store the result as a ExportVectorTilesResult.
    vii. Return the result's item resource cache.
  4. Update the Basemap and Viewpoint when a new style is selected.

Relevant API

  • ExportVectorTilesJob
  • ExportVectorTilesResult
  • ExportVectorTilesTask
  • VectorTileCache
  • VectorTiledLayer
  • VectorTilesTask

Offline data

To set up the sample's offline data, see the Use offline data in the samples section of the Qt Samples repository overview.

Link Local Location
DodgeCity vtpk File <userhome>/ArcGIS/Runtime/Data/vtpk/dodge_city.vtpk

Tags

tiles, vector, vector basemap, vector tiled layer, vector tiles

Sample Code

AddVectorTiledLayerFromCustomStyle.cppAddVectorTiledLayerFromCustomStyle.cppAddVectorTiledLayerFromCustomStyle.hAddVectorTiledLayerFromCustomStyle.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
// [WriteFile Name=AddVectorTiledLayerFromCustomStyle, Category=Layers]
// [Legal]
// Copyright 2025 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 "AddVectorTiledLayerFromCustomStyle.h"

#include "ArcGISVectorTiledLayer.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Viewpoint.h"
#include "Portal.h"
#include "PortalItem.h"
#include "ExportVectorTilesTask.h"
#include "ExportVectorTilesJob.h"
#include "ExportVectorTilesResult.h"
#include "ItemResourceCache.h"
#include "Basemap.h"
#include "VectorTileCache.h"

#include <QStandardPaths>
#include <QFuture>

using namespace Esri::ArcGISRuntime;

namespace
{
  QString defaultDataPath()
  {
#ifdef Q_OS_IOS
    return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
    return QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
#endif
  }
} // namespace

AddVectorTiledLayerFromCustomStyle::AddVectorTiledLayerFromCustomStyle(QObject* parent) :
  QObject(parent),
  m_dataPath(defaultDataPath() + "/ArcGIS/Runtime/Data/vtpk/dodge_city.vtpk"),
  m_map(new Map(this))
{
}

AddVectorTiledLayerFromCustomStyle::~AddVectorTiledLayerFromCustomStyle() = default;

void AddVectorTiledLayerFromCustomStyle::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<AddVectorTiledLayerFromCustomStyle>("Esri.Samples", 1, 0, "AddVectorTiledLayerFromCustomStyleSample");
}

MapQuickView* AddVectorTiledLayerFromCustomStyle::mapView() const
{
  return m_mapView;
}

void AddVectorTiledLayerFromCustomStyle::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
  {
    return;
  }

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  const Viewpoint defaultViewpoint{10, 5.5, 1e8};
  m_map->setInitialViewpoint(defaultViewpoint);

  populatePortalIdMap();
  emit mapViewChanged();
}

void AddVectorTiledLayerFromCustomStyle::populatePortalIdMap()
{
  // The labels and portal item IDs of the online styles.
  m_portalIds.insert("Default", "1349bfa0ed08485d8a92c442a3850b06");
  m_portalIds.insert("Style 1", "bd8ac41667014d98b933e97713ba8377");
  m_portalIds.insert("Style 2", "02f85ec376084c508b9c8e5a311724fa");
  m_portalIds.insert("Style 3", "1bf0cc4a4380468fbbff107e100f65a5");

  // The labels and portal item IDs of the offline styles.
  m_portalIds.insert("Offline custom style - Light", "e01262ef2a4f4d91897d9bbd3a9b1075");
  m_portalIds.insert("Offline custom style - Dark", "ce8a34e5d4ca4fa193a097511daa8855");

  setupPortalItemsAndExportStyles();

  m_styleNames = m_portalIds.keys();
  emit styleNamesChanged();
}

void AddVectorTiledLayerFromCustomStyle::setupPortalItemsAndExportStyles()
{
  Portal* portal = new Portal(this);

  // Store a list all portal items.
  for (const QString& itemID : m_portalIds.values())
  {
    PortalItem* portalItem = new PortalItem(portal, itemID, this);
    m_vectorTiledLayers.insert(m_portalIds.key(itemID), portalItem);
  }

  // Export offline custom styles.
  connect(this, &AddVectorTiledLayerFromCustomStyle::styleExported, this, [this](const QString& styleName, ItemResourceCache* cache)
  {
    if (styleName == "Light")
    {
      m_lightStyleResourceCache = cache;
    }
    else if (styleName == "Dark")
    {
      m_darkStyleResourceCache = cache;
    }
  });

  exportStyle(m_vectorTiledLayers.value("Offline custom style - Light"), "Light");
  exportStyle(m_vectorTiledLayers.value("Offline custom style - Dark"), "Dark");
}

void AddVectorTiledLayerFromCustomStyle::setStyle(const QString& style)
{
  // Check if the user selected an offline custom style.
  // Create a new basemap with the appropriate style.
  if (style.contains("Offline"))
  {
    ItemResourceCache* cache = style.contains("Light") ? m_lightStyleResourceCache : m_darkStyleResourceCache;

    VectorTileCache* vectorTileCache = new VectorTileCache(m_dataPath, this);

    ArcGISVectorTiledLayer* vectorTileLayer = new ArcGISVectorTiledLayer(vectorTileCache, cache, this);

    m_map->setBasemap(new Basemap(vectorTileLayer, this));

    const Viewpoint dodgeCityViewpoint{37.76528, -100.01766, 4e4};
    m_mapView->setViewpointAsync(dodgeCityViewpoint);
  }
  else
  {
    m_map->setBasemap(new Basemap(new ArcGISVectorTiledLayer(m_vectorTiledLayers.value(style))));
    const Viewpoint defaultViewpoint{10, 5.5, 1e8};
    m_mapView->setViewpointAsync(defaultViewpoint);
  }
}

void AddVectorTiledLayerFromCustomStyle::exportStyle(PortalItem* vectorTiledLayer, const QString& styleName)
{
  connect(vectorTiledLayer, &PortalItem::loadStatusChanged, this, [this, vectorTiledLayer, styleName](LoadStatus status)
  {
    if (status != LoadStatus::Loaded)
    {
      return;
    }

    // Create the task.
    ExportVectorTilesTask* exportTask = new ExportVectorTilesTask(vectorTiledLayer->url(), this);
    // Get the item resource path for the basemap styling.
    const QString itemResourceCachePath = m_tempDir.path() + QString("/itemResources%1").arg(QDateTime::currentMSecsSinceEpoch());

    // Create the export job and start it.
    Esri::ArcGISRuntime::ExportVectorTilesJob* exportJob = exportTask->exportStyleResourceCache(itemResourceCachePath);
    exportJob->start();

    // Wait for the job to complete.
    connect(exportJob, &ExportVectorTilesJob::jobDone, this, [this, exportJob, styleName]()
    {
      ExportVectorTilesResult* vectorTilesResult = exportJob->result();

      ItemResourceCache* cache = vectorTilesResult->itemResourceCache();

      if (styleName == "Light")
      {
        m_lightVectorTilesResult = vectorTilesResult;
      }
      else if (styleName == "Dark")
      {
        m_darkVectorTilesResult = vectorTilesResult;
      }

      connect(cache, &ItemResourceCache::loadStatusChanged, this, [this, cache, styleName](LoadStatus status)
      {
        if (status != LoadStatus::Loaded)
        {
          return;
        }
        emit styleExported(styleName, cache);
      });

      cache->load();
    });
  });
  vectorTiledLayer->load();
}

QStringList AddVectorTiledLayerFromCustomStyle::styleNames() const
{
  return m_styleNames;
}

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