Build a legend for all the operational layers in the map.

Use case
Legends are used to describe what each symbol on a map represents. A common format is to show an image of the symbol alongside of a text description of what that symbol represents. This sample demonstrates how a simple legend can be built up in code using the LayerContent interface.
How to use the sample
- Open the sample
- Flick through the legend control to see the various elements that represent features on the map.
How it works
- Layers implement the
LayerContentinterface, which contain a list ofLegendInfo.LegendInfocontains aSymboland a name string.Layer::setAutoFetchLegendInfosmust be called on eachLayerinstance to fetch the info from the data. - Connect to the
fetchLegendInfosCompletedsignal and once completed use theLegendInfoListModelto build the legend. - The names and images are then displayed next to each other in a list view.
Relevant API
- Layer::setAutoFetchLegendInfos
- LayerContent
- LegendInfo
- LegendInfoListModel
Tags
legend, legend info, symbol swatch, toolkit
Sample Code
// [WriteFile Name=BuildLegend, Category=DisplayInformation]// [Legal]// Copyright 2016 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 "BuildLegend.h"
// ArcGIS Maps SDK headers#include "ArcGISMapImageLayer.h"#include "Basemap.h"#include "FeatureLayer.h"#include "LayerListModel.h"#include "LegendInfoListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "Point.h"#include "ServiceFeatureTable.h"#include "SpatialReference.h"#include "Viewpoint.h"
// Qt headers#include <QUrl>
using namespace Esri::ArcGISRuntime;
BuildLegend::BuildLegend(QQuickItem* parent) : QQuickItem(parent){}
BuildLegend::~BuildLegend() = default;
void BuildLegend::init(){ qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<BuildLegend>("Esri.Samples", 1, 0, "BuildLegendSample"); qmlRegisterUncreatableType<QAbstractListModel>("Esri.Samples", 1, 0, "AbstractListModel", "AbstractListModel is uncreateable");}
void BuildLegend::componentComplete(){ QQuickItem::componentComplete();
// find QML MapView component m_mapView = findChild<MapQuickView*>("mapView");
// create a new basemap instance Basemap* basemap = new Basemap(BasemapStyle::ArcGISTopographic, this); // create a new map instance m_map = new Map(basemap, this); // set map to auto fetch LegendInfo m_map->setAutoFetchLegendInfos(true); // set initial viewpoint m_map->setInitialViewpoint(Viewpoint(Point(-11e6, 6e6, SpatialReference(3857)), 9e7)); // set map on the map view m_mapView->setMap(m_map);
addLayers();
// set the legend info list model m_legendInfoListModel = m_map->legendInfos(); emit legendInfoListModelChanged();}
void BuildLegend::addLayers(){ ArcGISMapImageLayer* mapImageLayer = new ArcGISMapImageLayer(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer"), this); m_map->operationalLayers()->append(mapImageLayer);
ServiceFeatureTable* featureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0"), this); FeatureLayer* featureLayer = new FeatureLayer(featureTable, this); m_map->operationalLayers()->append(featureLayer);}// [WriteFile Name=BuildLegend, Category=DisplayInformation]// [Legal]// Copyright 2016 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 BUILD_LEGEND_H#define BUILD_LEGEND_H
// Qt headers#include <QAbstractListModel>#include <QQuickItem>
namespace Esri::ArcGISRuntime{ class Map; class MapQuickView;} // namespace Esri::ArcGISRuntime
class QString;
class BuildLegend : public QQuickItem{ Q_OBJECT
Q_PROPERTY(QAbstractListModel* legendInfoListModel MEMBER m_legendInfoListModel NOTIFY legendInfoListModelChanged)
public: explicit BuildLegend(QQuickItem* parent = nullptr); ~BuildLegend() override;
void componentComplete() override; static void init();
signals: void legendInfoListModelChanged();
private: void addLayers();
private: Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; QAbstractListModel* m_legendInfoListModel = nullptr;};
#endif // BUILD_LEGEND_H// [WriteFile Name=BuildLegend, Category=DisplayInformation]// [Legal]// Copyright 2016 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
BuildLegendSample { id: showLegendSample width: 800 height: 600
// add a mapView component MapView { anchors.fill: parent objectName: "mapView"
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
// Create outter rectangle for the legend Rectangle { id: legendRect anchors { margins: 10 left: parent.left top: parent.top } property bool expanded: true height: 200 width: 175 color: palette.base opacity: 0.95 radius: 10 clip: true border { color: "darkgrey" width: 1 }
// Animate the expand and collapse of the legend Behavior on height { SpringAnimation { spring: 3 damping: .4 } }
// Catch mouse signals so they don't propagate to the map MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onClicked: mouse => mouse.accepted = true onDoubleClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true }
// Create UI for the user to select the layer to display Column { anchors { fill: parent margins: 10 } spacing: 2
Row { spacing: 55
Label { text: qsTr("Legend") font { pixelSize: 18 bold: true } }
// Legend icon to allow expanding and collapsing ToolButton { width: 24 height: 24 padding: 0 display: AbstractButton.IconOnly icon { source: legendRect.expanded ? "qrc:/Samples/DisplayInformation/BuildLegend/legend-24.svg" : "qrc:/Samples/DisplayInformation/BuildLegend/expand-24.svg"
width: 24 height: 24 color: palette.text } onClicked: { if (legendRect.expanded) { legendRect.height = 36; legendRect.expanded = false; } else { legendRect.height = 200; legendRect.expanded = true; } }
background: Rectangle { color: parent.hovered ? palette.midlight : "transparent" radius: 4 } } }
// Create a list view to display the legend ListView { id: legendListView anchors.margins: 10 model: showLegendSample.legendInfoListModel width: 165 height: 150 clip: true
// Create delegate to display the name with an image delegate: Item { width: parent ? parent.width : 0 height: 35 clip: true
Row { spacing: 5 anchors.verticalCenter: parent.verticalCenter
Image { anchors.verticalCenter: parent.verticalCenter width: 24 height: width source: symbolUrl } Label { anchors.verticalCenter: parent.verticalCenter width: 125 text: name wrapMode: Text.WordWrap font.pixelSize: 12 }
} }
section { property: "layerName" criteria: ViewSection.FullString labelPositioning: ViewSection.CurrentLabelAtStart | ViewSection.InlineLabels delegate: Rectangle { width: 180 height: childrenRect.height color: palette.highlight
Label { text: section font.bold: true font.pixelSize: 13 color: "#F8F8F8" } } } } } }}// [WriteFile Name=BuildLegend, Category=DisplayInformation]// [Legal]// Copyright 2015 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 "BuildLegend.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlEngine>#include <QQuickView>#include <QSettings>
// 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[]){ QGuiApplication app(argc, argv); app.setApplicationName(QString("Build Legend"));
// 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 BuildLegend::init();
// Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView);
// Add the import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
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
// Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath);
// Set the source view.setSource(QUrl("qrc:/Samples/DisplayInformation/BuildLegend/BuildLegend.qml"));
view.show();
return app.exec();}