Load ArcGIS vector tiled layers using custom styles.

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
- Create a
PortalItemfor each vector tiled layer. - Create a
Mapand set the defaultViewpoint. - Export the light and dark offline custom styles.
i. Create aExportVectorTilesTaskusing 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 aExportVectorTilesJobby having the task callExportStyleResourceCachewith the path as a parameter.
v. Start the job.
vi. When the job completes, store the result as aExportVectorTilesResult.
vii. Return the result’s item resource cache. - Update the
BasemapandViewpointwhen 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
// [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;}// [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]
#ifndef ADDVECTORTILEDLAYERFROMCUSTOMSTYLE_H#define ADDVECTORTILEDLAYERFROMCUSTOMSTYLE_H
#include <QTemporaryDir>
namespace Esri::ArcGISRuntime{ class ExportVectorTilesJob; class ExportVectorTilesResult; class ItemResourceCache; class Map; class MapQuickView; class PortalItem; class Viewpoint;} // namespace Esri::ArcGISRuntime
Q_MOC_INCLUDE("MapQuickView.h");
class AddVectorTiledLayerFromCustomStyle : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(QStringList styleNames READ styleNames NOTIFY styleNamesChanged)
public: explicit AddVectorTiledLayerFromCustomStyle(QObject* parent = nullptr); ~AddVectorTiledLayerFromCustomStyle() override;
static void init(); Q_INVOKABLE void setStyle(const QString& style);
signals: void mapViewChanged(); void styleNamesChanged(); void styleExported(const QString& styleName, Esri::ArcGISRuntime::ItemResourceCache* cache);
private: void exportStyle(Esri::ArcGISRuntime::PortalItem* vectorTiledLayer, const QString& styleName); void setupPortalItemsAndExportStyles(); Esri::ArcGISRuntime::MapQuickView* mapView() const; void populatePortalIdMap(); void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); QStringList styleNames() const;
const QString m_dataPath; Esri::ArcGISRuntime::ExportVectorTilesResult* m_darkVectorTilesResult = nullptr; Esri::ArcGISRuntime::ItemResourceCache* m_darkStyleResourceCache = nullptr; Esri::ArcGISRuntime::ExportVectorTilesJob* m_exportJob = nullptr; Esri::ArcGISRuntime::ItemResourceCache* m_lightStyleResourceCache = nullptr; Esri::ArcGISRuntime::ExportVectorTilesResult* m_lightVectorTilesResult = nullptr; Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; QMap<QString, QString> m_portalIds; QStringList m_styleNames; QTemporaryDir m_tempDir; QMap<QString, Esri::ArcGISRuntime::PortalItem*> m_vectorTiledLayers;};
#endif // ADDVECTORTILEDLAYERFROMCUSTOMSTYLE_H// [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]
import QtQuickimport QtQuick.Controlsimport Esri.Samples
Item { // add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set and keep the focus on MapView to enable keyboard navigation forceActiveFocus(); } }
// Declare the C++ instance which creates the map etc. and supply the view AddVectorTiledLayerFromCustomStyleSample { id: model mapView: view }
Rectangle { anchors { top: parent.top right: parent.right margins: 5 } width: childrenRect.width height: childrenRect.height color: palette.base MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onClicked: mouse => mouse.accepted = true onDoubleClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true } Column { id: settingsColumn spacing: 5 padding: 15 Label { id: selectStyle anchors.horizontalCenter: parent.horizontalCenter font.pixelSize: 16 text: qsTr("Select style") } ComboBox { id: layerStyle anchors.horizontalCenter: parent.horizontalCenter width: 250 model: model.styleNames
onCurrentTextChanged: model.setStyle(currentText); } } }}// 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.
#include "AddVectorTiledLayerFromCustomStyle.h"
#include "ArcGISRuntimeEnvironment.h"
#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
#ifdef Q_OS_WIN#include <Windows.h>#endif
int main(int argc, char* argv[]){ QGuiApplication app(argc, argv); app.setApplicationName(QString("AddVectorTiledLayerFromCustomStyle"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample AddVectorTiledLayerFromCustomStyle::init();
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
#ifdef ARCGIS_RUNTIME_IMPORT_PATH_2 engine.addImportPath(ARCGIS_RUNTIME_IMPORT_PATH_2);#endif
// Set the source engine.load(QUrl("qrc:/Samples/Layers/AddVectorTiledLayerFromCustomStyle/main.qml"));
return app.exec();}// 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.
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
AddVectorTiledLayerFromCustomStyle { anchors.fill: parent }}