Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to add feature layers to a 2D map.

Applications can access and display feature layers that are hosted on ArcGIS Online or ArcGIS Enterprise. A hosted feature layer contains features with the same geometry and attributes, and has its own REST endpoint at a unique URL. The FeatureLayer class can use this URL to draw point, polyline, or polygon features on a map. By default, feature layers will draw symbols to fill the extent of the map, but you can customize this by setting your own styles with a renderer or using an SQL expression to limit which features are displayed on the map.

In this tutorial, you will build a simple app with a basemap and a feature layer of trailheads near the Santa Monica Mountains. This layer is hosted as an item on 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

Add a layer to the map

  1. In the Projects window, double click on Sources > Create_a_starter_app.cpp and add the header files for ServiceFeatureTable and FeatureLayer.

    #include "Basemap.h"
    #include "Map.h"
    #include "MapQuickView.h"
    
    // *** Add ***
    #include "ServiceFeatureTable.h"
    #include "FeatureLayer.h"
    
  2. Add the following code to the body of the constructor to create a QUrl object with the URL of the trail heads feature service.

     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);
       m_map->setInitialViewpoint(viewpoint);
    
       // *** ADD ***
       const QUrl url("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0");
    
  3. Add the following code to the end of the constructor body to create a feature layer from the feature service URL and then append this feature layer pointer to the operational layers of the map.

     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);
       m_map->setInitialViewpoint(viewpoint);
    
       const QUrl url("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0");
    
       // *** ADD ***
       ServiceFeatureTable* featureTable = new ServiceFeatureTable(url, this);
       FeatureLayer* featureLayer = new FeatureLayer(featureTable, this);
       m_map->operationalLayers()->append(featureLayer);
    
  4. Run your code to display the feature layer.

Congratulations, you're done!

Your map should load and center on the Santa Monica Mountains with a separate feature layer that shows the trail head symbols represented as orange markers on the map. Compare your map with our completed solution project.

Challenge

Create and import data

The Discover data tutorial shows you how to create new datasets for your apps using ArcGIS Online. Create an empty dataset, add some data, and add it to your app.

The Import data tutorial shows you how to import your own data, stored in formats like CSV, GeoJSON, or shapefiles, as feature layers.

Add an Open Street Map layer

Try loading operational layers other than feature layers, like raster and Open Street Map layers. For example, try loading an Open Street Map layer as shown in the Open Street Map code sample and then append this layer to the m_map private member variable.

See the layer types guide topic for a list of all the layer types supported by the ArcGIS Runtime SDK for Qt C++.

Change basemap

Change the basemap of your map by examining the ChangeBasemap.cpp source file in the basemap code sample. Customize your solution with the basemap you like best.