Group a collection of layers together and toggle their visibility as a group.

Use case
Group layers communicate to the user that layers are related and can be managed together.
In a land development project, you might group layers according to the phase of development.
How to use the sample
The layers in the map will be displayed in a table of contents. Toggle the checkbox next to a layer’s name to change its visibility. Turning a group layer’s visibility off will override the visibility of its child layers.
How it works
- Create an empty
GroupLayer. - Create a new layer and add it to the group layer’s layers collection.
- Set the group layer’s
GroupVisibilityModeto change its behavior:
GroupVisibilityMode::Independentallows each sublayer to change its visibility independently.GroupVisibilityMode::Exclusiveallows only one sublayer to be visible at a time.GroupVisibilityMode::Inheritedtreats the group layer as if it is one merged layer.
- Create a ListView and bind the model the the scene’s operational layer list model.
- Create a delegate for the group layer and and another delegate for all other layers. The group layer delegate will have nested checkboxes.
- Check that the layer is a group layer. If true, load the group layer delegate. If false, load the other delegate.
- Each delegate will use the
nameandlayerVisibleroles exposed on theLayerListModel. - To toggle the visibility of the group, simply change the group layer’s visibility property.
Relevant API
- GroupLayer
Additional information
The full extent of a group layer may change when child layers are added/removed. Group layers do not have a spatial reference, but the full extent will have the spatial reference of the first child layer.
Tags
group layer
Sample Code
// [WriteFile Name=GroupLayers, Category=Layers]// [Legal]// Copyright 2019 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 "GroupLayers.h"
// ArcGIS Maps SDK headers#include "ArcGISSceneLayer.h"#include "ArcGISTiledElevationSource.h"#include "ElevationSourceListModel.h"#include "Envelope.h"#include "Error.h"#include "FeatureLayer.h"#include "GroupLayer.h"#include "LayerListModel.h"#include "MapTypes.h"#include "Scene.h"#include "SceneQuickView.h"#include "ServiceFeatureTable.h"#include "Surface.h"#include "Viewpoint.h"
// Qt headers#include <QFuture>
using namespace Esri::ArcGISRuntime;
GroupLayers::GroupLayers(QObject* parent /* = nullptr */): QObject(parent), m_scene(new Scene(BasemapStyle::ArcGISImageryStandard, this)){ // create a new elevation source from Terrain3D REST service ArcGISTiledElevationSource* elevationSource = new ArcGISTiledElevationSource( QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this);
// add the elevation source to the scene to display elevation m_scene->baseSurface()->elevationSources()->append(elevationSource);
// Create layers and append to a group layer ArcGISSceneLayer* layer1 = new ArcGISSceneLayer(QUrl("https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/DevA_BuildingShells/SceneServer"), this); ArcGISSceneLayer* layer2 = new ArcGISSceneLayer(QUrl("https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/DevB_BuildingShells/SceneServer"), this); GroupLayer* groupLayer = new GroupLayer(QList<Layer*>{layer1, layer2}, this); groupLayer->setName("Buildings group"); groupLayer->setVisibilityMode(GroupVisibilityMode::Exclusive);
// Create additional layers ArcGISSceneLayer* layer3 = new ArcGISSceneLayer(QUrl("https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/DevA_Trees/SceneServer"), this); ServiceFeatureTable* ft1 = new ServiceFeatureTable(QUrl("https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/DevelopmentProjectArea/FeatureServer/0"), this); FeatureLayer* layer4 = new FeatureLayer(ft1, this); ServiceFeatureTable* ft2 = new ServiceFeatureTable(QUrl("https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/DevA_Pathways/FeatureServer/1"), this); FeatureLayer* layer5 = new FeatureLayer(ft2, this);
// Add layers to the scene m_scene->operationalLayers()->append(groupLayer); m_scene->operationalLayers()->append(layer3); m_scene->operationalLayers()->append(layer4); m_scene->operationalLayers()->append(layer5);
connect(m_scene, &Scene::doneLoading, this, [this](const Error& e) { if (!e.isEmpty()) return;
m_layerListModel = m_scene->operationalLayers(); emit layerListModelChanged(); });}
GroupLayers::~GroupLayers() = default;
void GroupLayers::init(){ // Register classes for QML qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView"); qmlRegisterType<GroupLayers>("Esri.Samples", 1, 0, "GroupLayersSample"); qmlRegisterUncreatableType<LayerListModel>("Esri.Samples", 1, 0, "AbstractListModel", "AbstractListModel is uncreateable");}
SceneQuickView* GroupLayers::sceneView() const{ return m_sceneView;}
// Set the view (created in QML)void GroupLayers::setSceneView(SceneQuickView* sceneView){ if (!sceneView || sceneView == m_sceneView) { return; }
m_sceneView = sceneView; m_sceneView->setArcGISScene(m_scene);
// create initial viewpoint Geometry env = Geometry::fromJson(R"({"spatialReference":{ "wkid":4326 }, "xmax":-122.67960721754773, "xmin":-122.68647066116789, "ymax":45.53584958588318, "ymin":45.531539857343745 })"); // zoom to viewpoint m_sceneView->setViewpointAsync(Viewpoint(env.extent()));
emit sceneViewChanged();}
LayerListModel* GroupLayers::getGroupLayerListModel(int layerId){ if (!m_scene) return nullptr;
GroupLayer* groupLayer = static_cast<GroupLayer*>(m_scene->operationalLayers()->at(layerId));
if (groupLayer) return groupLayer->layers();
return nullptr;}// [WriteFile Name=GroupLayers, Category=Layers]// [Legal]// Copyright 2019 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 GROUPLAYERS_H#define GROUPLAYERS_H
// Qt headers#include <QObject>
namespace Esri::ArcGISRuntime{class Scene;class SceneQuickView;class LayerListModel;}
class QAbstractListModel;
Q_MOC_INCLUDE("SceneQuickView.h")Q_MOC_INCLUDE("LayerListModel.h")
class GroupLayers : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::SceneQuickView* sceneView READ sceneView WRITE setSceneView NOTIFY sceneViewChanged) Q_PROPERTY(Esri::ArcGISRuntime::LayerListModel* layerListModel MEMBER m_layerListModel NOTIFY layerListModelChanged)
public: explicit GroupLayers(QObject* parent = nullptr); ~GroupLayers();
static void init();
signals: void sceneViewChanged(); void layerListModelChanged();
public: Q_INVOKABLE Esri::ArcGISRuntime::LayerListModel* getGroupLayerListModel(int layerId);
private: Esri::ArcGISRuntime::SceneQuickView* sceneView() const; void setSceneView(Esri::ArcGISRuntime::SceneQuickView* sceneView);
Esri::ArcGISRuntime::Scene* m_scene = nullptr; Esri::ArcGISRuntime::SceneQuickView* m_sceneView = nullptr; Esri::ArcGISRuntime::LayerListModel* m_layerListModel = nullptr;};
#endif // GROUPLAYERS_H// [WriteFile Name=GroupLayers, Category=Layers]// [Legal]// Copyright 2019 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 {
SceneView { id: view anchors.fill: parent
Component.onCompleted: { // Set the focus on SceneView to initially enable keyboard navigation forceActiveFocus(); } }
// Declare the C++ instance which creates the scene etc. and supply the view GroupLayersSample { id: sampleModel sceneView: view }
// Create a window to display the layers Rectangle { id: layerVisibilityRect anchors { fill: layerVisibilityListView margins: -5 } color: "lightgrey" border.color: "#4D4D4D" opacity: 0.9 radius: 5 }
// Create a list view to display the items ListView { id: layerVisibilityListView anchors { left: parent.left top: parent.top margins: 10 } width: 250 height: childrenRect.height clip: true
// Assign the model to the list model of layers model: sampleModel.layerListModel
// Assign the delegate to the delegate created above delegate: Item { height: childrenRect.height
// select the component based on the layer type // GroupLayer is LayerType int value of 22. See API doc for more details: // https://developers.arcgis.com/qt/layers/ Loader { sourceComponent: layerType === 22 ? groupLayerDelegate : layerDelegate }
// Delegate for GroupLayers - Contains secondary repeater for sublayers Component { id: groupLayerDelegate Column { CheckBox { id: parentBox text: name checked: layerVisible onCheckedChanged: layerVisible = checked }
Repeater { model: sampleModel.getGroupLayerListModel(layerVisibilityListView.currentIndex) delegate: RadioDelegate { checked: index === 0 text: name leftPadding: indicator.width width: layerVisibilityRect.width - leftPadding onCheckedChanged: layerVisible = checked } } } }
// Delegate for all other layers - standard Checkbox Component { id: layerDelegate CheckBox { text: name checked: layerVisible onCheckedChanged: layerVisible = checked; } } } }}// [Legal]// Copyright 2019 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]
// sample headers#include "GroupLayers.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>#include <QSurfaceFormat>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false);#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) // Linux requires 3.2 OpenGL Context // in order to instance 3D symbols QSurfaceFormat fmt = QSurfaceFormat::defaultFormat(); fmt.setVersion(3, 2); QSurfaceFormat::setDefaultFormat(fmt);#endif
QGuiApplication app(argc, argv); app.setApplicationName(QString("GroupLayers"));
// 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 GroupLayers::init();
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive);#endif
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path engine.addImportPath(arcGISRuntimeImportPath);
// Set the source engine.load(QUrl("qrc:/Samples/Layers/GroupLayers/main.qml"));
return app.exec();}// Copyright 2019 ESRI//// All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.//// You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.//// See the Sample code usage restrictions document for further information.//
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
GroupLayers { anchors.fill: parent }}