Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to load and display a pre-configured layer from ArcGIS Online.

Applications can access layers that are hosted on ArcGIS Online or ArcGIS Enterprise. Each layer items contains the settings for the hosted feature layer such as the URL to access the data, the styles used to draw the layer, the pop-up configuration, and the data filters. These settings can be pre-configured interactively using portal tools. Applications can load layer items by accessing a portal and requesting the item by ID. The benefit of loading a layer item is that all of the layer settings will be applied when the item is loaded, saving you time from having to set them up yourself. Learn more about working with layers in the Configure layers tutorial.

In this tutorial, you will add layers to the map that have been pre-configured and stored as an item in ArcGIS Online.

Before you begin

You must have previously installed the ArcGIS Runtime SDK for Qt and set up the development environment for your operating system.

Open the starter app project

If you have already completed the Create a starter app tutorial, start Qt Creator and open your starter app project. Otherwise, download and unzip the starter app project solution, and then open it in Qt Creator.

Steps

Find the ID of the Trailheads layer

  1. In your browser, go to the Trailheads Styled layer on ArcGIS Online and find the item ID at the end of the URL. It should be 2e4b3df6ba4b44969a3bc9827de746b3.

Import headers and declare member variables

  1. In the Projects window, double click on Headers > Create_a_starter_app.h, add the namespace declaration to include Portal and PortalItem.

    namespace Esri
    {
    namespace ArcGISRuntime
    {
    class Map;
    class MapQuickView;
    
    // *** ADD ***
    class Portal;
    class PortalItem;
    
  2. Add two new private member variables to the same header file.

    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
    
      Esri::ArcGISRuntime::Map* m_map = nullptr;
      Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
    
      // *** ADD ***
      Esri::ArcGISRuntime::Portal* m_portal = nullptr;
      Esri::ArcGISRuntime::PortalItem* m_portalItem = nullptr;
    

Load the portal

Create an anonymous connection to ArcGIS Online, which is a portal hosted by Esri, retrieve a feature layer as an item from the portal, and then add the feature layer to the map.

  1. In Projects, double click on Sources > Create_a_starter_app.cpp and add three new header files.

    #include "MapQuickView.h"
    
    // *** ADD ***
    #include "Portal.h"
    #include "PortalItem.h"
    #include "FeatureLayer.h"
    
  2. Add the following code to the constructor in order to load the Portal for ArcGIS Online.

    Create_a_starter_app::Create_a_starter_app(QObject* parent /* = nullptr */):
      QObject(parent),
      m_map(new Map(Basemap::topographicVector(this), this))
    {
      const Point center(-118.71511, 34.09042, SpatialReference::wgs84());
      const Viewpoint viewpoint(center, 300000.0);
    
      // Set the initial viewpoint
      m_map->setInitialViewpoint(viewpoint);
    
      // *** ADD ***
      const QUrl portal_url("https://www.arcgis.com");
      m_portal = new Portal(portal_url, this);
    
      connect(m_portal, &Portal::doneLoading, this, [](Esri::ArcGISRuntime::Error loadError)
        {
          if (!loadError.isEmpty())
            qDebug() << loadError.message();
        }
      );
    

Load a portal item

  1. Add the following code to the constructor to load a PortalItem from the ArcGIS Online portal.

       Create_a_starter_app::Create_a_starter_app(QObject* parent /* = nullptr */):
         QObject(parent),
         m_map(new Map(Basemap::topographicVector(this), this))
       {
         const Point center(-118.71511, 34.09042, SpatialReference::wgs84());
         const Viewpoint viewpoint(center, 300000.0);
    
        // Set the initial viewpoint
        m_map->setInitialViewpoint(viewpoint);
    
        const QUrl portal_url("https://www.arcgis.com");
        m_portal = new Portal(portal_url, this);
    
        connect(m_portal, &Portal::doneLoading, this, [](Esri::ArcGISRuntime::Error loadError)
          {
            if (!loadError.isEmpty())
              qDebug() << loadError.message();
          }
        );
    
        // *** ADD ***
        const QString portal_id("2e4b3df6ba4b44969a3bc9827de746b3");
    
        m_portalItem = new PortalItem(m_portal, portal_id, this);
    
        connect(m_portalItem, &PortalItem::doneLoading, this, [this]()
          {
            qDebug() << "Item title:" << this->m_portalItem->title();
          }
        );
    

Add the feature layer

  1. In the Create_a_starter_app::setMapView method, add the following code to create a feature layer and append it to the map.

     void Add_a_layer_from_an_item::setMapView(MapQuickView* mapView)
     {
       if (!mapView || mapView == m_mapView)
       {
           return;
       }
    
       m_mapView = mapView;
       m_mapView->setMap(m_map);
    
       // *** ADD ***
       FeatureLayer* layer = new FeatureLayer(m_portalItem, 0, this);
       m_map->operationalLayers()->append(layer);
    
  2. Run your code to view the map.

Congratulations, you're done!

Your app should display a map with the trailheads centered on the Santa Monica Mountains. Compare your solution with our completed solution project.

Challenge

Load a feature layer by URL

There are multiple ways to load a feature layer from ArcGIS Online. One way is to load the feature layer directly from a URL.

Navigate to the page in ArcGIS Online for your portal item. In the item page, find the Service URL link and identify the service endpoint. This is the URL the runtime uses to access the data. It should look something like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0

Create a ServiceFeatureTable with this URL (where this references the parent class which inherits QObject).

  const QUrl url("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0");
  ServiceFeatureTable* table = new ServiceFeatureTable(url, this);

Create the FeatureLayer from the service feature table.

  FeatureLayer* layerFromTable = new FeatureLayer(table, this);

Add the layer to the map.

  m_map->operationalLayers()->append(layerFromTable);

Learn how to create portal items with your own data in the Create a new dataset tutorial.

Add the Trails and Parks and Open Space layers by ID

Use the same code pattern in step 5 and 7 to load two more layers into the map. Go to the Trails Styled and the Parks and Open Space Styled layers to find the item IDs and add the layers to the map.

Discover data

If you have not already done so, visit the Discover data tutorial and search for open data you could use to add another layer to your app.