Learn how to use an ArcGIS portal item to access and display a feature layer in a map.

You can host a variety of geographic data and other resources using ArcGIS Online. These portal items can also define how the data is presented. A web map or web scene, for example, not only defines the layers for a map or scene, 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 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
The following are required for this tutorial:
- An ArcGIS account to access API keys. If you don't have an account, sign up for free.
- Your system meets the system requirements.
- The ArcGIS Maps SDK for Qt, version 200.2.0 or later is installed.
- The Qt 6.5.1 software development framework is installed.
Steps
Open the project in Qt Creator
-
To start this tutorial, complete the Display a map tutorial or download and unzip the solution.
-
Open the display_a_map project in Qt Creator.
-
If you downloaded the Display a map solution, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
-
Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
-
In the Projects window, in the Sources folder, open the main.cpp file.
-
Modify the code to set the API key. Paste the API key, acquired from your dashboard, between the quotes. Then save and close the file.
main.cppUse dark colors for code blocks Change line // 2. API key authentication: Get a permanent key that gives your application access to Esri // location services. Create a new API key or access existing API keys from // your ArcGIS for Developers dashboard (https://links.esri.com/arcgis-api-keys). const QString apiKey = QString("");
-
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, open the Sources folder and open the Display_a_map.cpp file. Add the following four include statements.
Display-a-map.cppUse dark colors for code blocks Add line. Add line. Add line. Add line. #include "Display_a_map.h" #include "Map.h" #include "MapTypes.h" #include "MapQuickView.h" #include "Point.h" #include "Viewpoint.h" #include "SpatialReference.h" #include "TaskWatcher.h" #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. You can then create a FeatureLayer
to display the PortalItem
in the map.
-
Create a
Portal
instance using the default constructor.Display-a-map.cppUse dark colors for code blocks Add line. const Point center(-118.80543, 34.02700, SpatialReference::wgs84()); const Viewpoint viewpoint(center, 100000.0); m_mapView->setViewpoint(viewpoint); Portal* portal = new Portal(this);
-
Create a
PortalItem
that references the trailheads item ID on thePortal
.Display-a-map.cppUse dark colors for code blocks Add line. Add line. Portal* portal = new Portal(this); const QString itemId("2e4b3df6ba4b44969a3bc9827de746b3"); PortalItem* portalItem = new PortalItem(portal, itemId, this);
-
Create a
FeatureLayer
, using thePortalItem
and referencing aservice
ofLayer I d 0
(zero). Then append this layer to the map's data layers (operational layers). Save the file.Display-a-map.cppUse dark colors for code blocks Add line. Add line. const QString itemId("2e4b3df6ba4b44969a3bc9827de746b3"); PortalItem* portalItem = new PortalItem(portal, itemId, this); FeatureLayer* trailheadsLayer = new FeatureLayer(portalItem, 0, this); m_map->operationalLayers()->append(trailheadsLayer);
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. Some tutorials also include instructions for QML.