Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to build an app that loads and displays a layer from ArcGIS Online.

Applications can access layers that are hosted on ArcGIS Online or ArcGIS Enterprise. Each layer item 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 AppStudio for ArcGIS.

Open the starter app project

If you completed the Create a starter app tutorial, start AppStudio and then open your starter app project. Otherwise, complete the tutorial now and open the project.

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 33fc2fa407ab40f9add12fe38d5801f5.

Declare properties

  1. Run AppStudio. Click your Starter app project in the App Gallery, and then click {;} Edit. This will open your project in Qt Creator.

  2. In the Qt Creator Projects window, double-click MyApp.qml to open it in the editor. In the App declaration, add the following properties to specify the URL of the portal and the layer item ID.

    property real scaleFactor: AppFramework.displayScaleFactor
    
    // *** ADD ***
    property string portalUrl: "https://arcgis.com";
    property string trailheadsItemID: "33fc2fa407ab40f9add12fe38d5801f5"
    

Load the portal

  1. In the App declaration, just below the properties, declare a Portal type to create an unauthenticated connection to the ArcGIS Online portal.

     property string trailheadsItemID: "33fc2fa407ab40f9add12fe38d5801f5"
    
     // *** ADD ***
     Portal {
         id: arcgisOnline
         url: portalUrl
         loginRequired: false
     }
    

Load a portal item

  1. After the Portal declaration, declare a PortalItem type to load the layer with the Trailheads Styled item ID from the ArcGIS Online portal.

    PortalItem {
        id: portalItemTrailheads
        portal: arcgisOnline
        itemId: trailheadsItemID
    }
    

Add the feature layer

  1. In the code editor, scroll down to the Map declaration. Add code after the basemap declaration to create a FeatureLayer from the portal item.

     Map {
       id:map
    
       BasemapTopographicVector {}
    
       // *** ADD ***
       FeatureLayer {
         id:trailheadFeatureLayer
         item: portalItemTrailheads
         serviceLayerId: "0"
       }
    
  2. After the declaration of FeatureLayer, add a callback to append the feature layer to the operationalLayers of the map when the layer loads.

     FeatureLayer {
         id:trailheadFeatureLayer
         item: portalItemTrailheads
         serviceLayerId: "0"
     }
    
     // *** ADD ***
     onLoadStatusChanged: {
         if (loadStatus === Enums.LoadStatusLoaded) {
             map.operationalLayers.append(trailheadFeatureLayer);
         }
     }
    
  3. In the lower left Sidebar, click Run or press Control-R/Command-R to run the app.

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

Add the Trails and Parks and Open Space layers by item ID

Use the same code pattern 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 as additional operational layers.

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. Learn how to declare ServiceFeatureTable with a URL and create a FeatureLayer from the service feature table in the Add layers to a map tutorial.

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

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.