Learn how to use an ArcGIS portal item to access and display a feature layer A feature layer (client-side) is a data layer that can access and display features from a feature service that has the same type of geometry and attribute fields. Learn more in a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more .

Qt app window showing a map of the Santa Monica Mountains with hiking trailhead symbols.

You can host a variety of geographic data and other resources using ArcGIS Online ArcGIS Online is a GIS mapping, analytics, data hosting, and content management software as a service (SaaS) product. It includes applications, tools, APIs, and location services for users and developers. It is subscription-based and requires an ArcGIS Online account. Learn more . These portal items An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. Learn more can also define how the data is presented. A web map A web map is a map stored as a JSON object that defines properties such as the basemap layer, data layers, layer styles, and pop-up styles. Its JSON structure is defined by the web map specification. Learn more or web scene A web scene is a scene stored as a JSON object that defines properties such as the basemap layer, data layers, layer styles, and pop-up styles. Its JSON structure is defined by the web scene specification. Learn more , for example, not only defines the layers for a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more or scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more , but also how layers are symbolized, the minimum and/or maximum scales at which they display, and several other properties. Likewise, a hosted feature layer contains the data for the layer and also defines the symbols and other display properties for how it is presented. When you add a map, scene, or layer from a portal item to your app, everything that has been saved with the item An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. Learn more is applied in your app. Adding portal items to your app rather than creating them programmatically saves you from writing a lot of code, and can provide consistency across apps that use the same data.

In this tutorial, you will add a hosted feature layer to display trailheads in the Santa Monica Mountains of Southern California. The hosted layer defines the trailhead locations (points) as well as the symbols used to display them.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Your system meets the system requirements.

  3. The ArcGIS Maps SDK for Qt, version 300.1.0 or later is installed.

  4. The Qt 6.8.2 software development framework or later is installed.

  5. The CMake build system is installed.

Develop or Download

You have two options for completing this tutorial:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution

Option 1: Develop the code

To start the tutorial, complete the Display a map tutorial. This creates a map to display the Santa Monica Mountains in California using the topographic basemap from the ArcGIS Basemap Styles service The ArcGIS Basemap Styles service, also referred to as the Basemap Styles service, is a location service that provides basemap styles and data for the world. It returns styles as Mapbox styles and web maps, and data as vector tiles and/or map tiles. It supports all of the styles in the ArcGIS Basemap style and Open Basemap style family. An ArcGIS Location Platform or ArcGIS Online account is required to use the service. Learn more .

Open a Qt Creator project

  1. Open the project you created by completing the Display a map tutorial.
  2. Continue with the following instructions to use an ArcGIS portal item to access and display a feature layer in a map.

Include header files

Your app needs the Portal and PortalItem classes to load an item hosted on ArcGIS Online and display it as a FeatureLayer in the map.

In the project Display_a_map > Display_a_map > Source Files folder of Qt Creator, double-click the Display_a_map.cpp file to open it. Add the following four include statements.

Display-a-map.cpp
#include "Display_a_map.h"
#include "Map.h"
#include "MapTypes.h"
#include "MapQuickView.h"
#include "Point.h"
#include "Viewpoint.h"
#include "SpatialReference.h"
#include <QFuture>
#include "Portal.h"
#include "PortalItem.h"
#include "FeatureLayer.h"
#include "LayerListModel.h"

Add the trailheads feature layer to the map

You will connect to ArcGIS Online and access a hosted item (trailheads layer) using its item ID An item ID is a unique identifier representing a single item stored, managed, and accessed in a portal, such as a web map, hosted layer, or file. Learn more . You can then create a FeatureLayer to display the PortalItem in the map.

Staying in the Display_a_map.cpp file, within the setupViewpoint() method, create a Portal instance using the default constructor.

Display-a-map.cpp
void Display_a_map::setupViewpoint()
{
const Point center(-118.80543, 34.02700, SpatialReference::wgs84());
const Viewpoint viewpoint(center, 100000.0);
m_mapView->setViewpointAsync(viewpoint);
Portal* portal = new Portal(this);
}

Continuing in the setupViewpoint() method, add code to create a PortalItem that references the trailheads item ID on the Portal.

Display-a-map.cpp
void Display_a_map::setupViewpoint()
{
const Point center(-118.80543, 34.02700, SpatialReference::wgs84());
const Viewpoint viewpoint(center, 100000.0);
m_mapView->setViewpointAsync(viewpoint);
Portal* portal = new Portal(this);
const QString itemId("2e4b3df6ba4b44969a3bc9827de746b3");
PortalItem* portalItem = new PortalItem(portal, itemId, this);
}

Concluding in the setupViewpoint() method, add code to create a FeatureLayer, using the PortalItem and referencing a serviceLayerId of 0 (zero). Then append this layer to the map’s data layers (operational layers). Save the file.

Display-a-map.cpp
void Display_a_map::setupViewpoint()
{
const Point center(-118.80543, 34.02700, SpatialReference::wgs84());
const Viewpoint viewpoint(center, 100000.0);
m_mapView->setViewpointAsync(viewpoint);
Portal* portal = new Portal(this);
const QString itemId("2e4b3df6ba4b44969a3bc9827de746b3");
PortalItem* portalItem = new PortalItem(portal, itemId, this);
FeatureLayer* trailheadsLayer = new FeatureLayer(portalItem, 0, this);
m_map->operationalLayers()->append(trailheadsLayer);
}

Set developer credentials

For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.

Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.

Set the API Key

In the project Display_a_map > Display_a_map > Source Files folder of Qt Creator, double click the main.cpp file to open it. Modify the code to set the accessToken variable using your API key access token. Then save the file.

main.cpp
// 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
{
ArcGISRuntimeEnvironment::setApiKey(accessToken);
}

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Press Ctrl + R to run the app.

Your app should display a map with the trailheads centered on the Santa Monica Mountains. Double-click, drag, and scroll the mouse wheel over the map view to explore the map.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution

Click the Download solution link on this page under the Solution title and unzip the file to a location on your machine.

In Qt Creator, choose File > Open File or Project… from them menus. In the Open file dialog, navigate to the CMakeLists.txt file for the solution you unzipped and click the Open button.

The Configure Project window will appear for you to specify the Qt Kit to use to compile the project. Check on the Qt Kit called Desktop Qt 6.8.2 MSVC2022 64bit before clicking the Configure Project button at the bottom of the page.

Since the downloaded solution does not contain authentication credentials (API Key or User authentication), you must set up authentication to create the developer credentials and add them to the project.

For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.

Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.

Set developer credentials in the solution

Set the API Key

In the project Display_a_map > Display_a_map > Source Files folder of Qt Creator, double click the main.cpp file to open it. Modify the code to set the accessToken variable using your API key access token. Then save the file.

main.cpp
// 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
{
ArcGISRuntimeEnvironment::setApiKey(accessToken);
}

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run the solution

Press Ctrl + R to run the app.

Your app should display a map with the trailheads centered on the Santa Monica Mountains. Double-click, drag, and scroll the mouse wheel over the map view to explore the map.

What’s next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials.