Add layers to a map

Overview

You will learn: how to build an app that displays feature layers in 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 QML type can use this URL to draw point, polyline, or polygon features on a map. By default, feature layers will draw all features within 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 add the Trailheads, Trails, and Parks and Open Space hosted feature layers to the map.

Before you begin

You must have previously installed ArcGIS AppStudio.

Open the starter app project

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

Steps

Add a layer to the map

  1. Run ArcGIS 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 on MyApp.qml to open it in the editor. In the App declaration, add the following code to declare a property with the URL of the trail heads feature service.

    ::: labs-info You can learn how to locate the URL for a feature service in the manage a feature layer tutorial. :::

    Use dark colors for code blocksCopy
        
    1
    2
    3
    4
    property real scaleFactor: AppFramework.displayScaleFactor
    
    // *** ADD ***
    property string trailheadsURL: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"
  3. In the code editor, scroll down to the Map declaration. Add the following code to create a FeatureLayer from the feature service URL and add it to the operational layers of the map.

    ::: labs-info The trail head features are stored online in feature tables and are accessed through a feature service. The ServiceFeatureTable QML type accesses the feature table, and the FeatureLayer QML type creates a feature layer from the feature table. This layer is then added to the operational layers of the map. In this way, feature layers can be created from different feature tables, and multiple layers can be added to the same map. :::

    Use dark colors for code blocksCopy
                
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
           Map {
             id:map
    
             BasemapTopographicVector {}
    
             // *** ADD ***
             // add a feature layer of Santa Monica trail heads
             FeatureLayer {
                 ServiceFeatureTable {
                     url: trailheadsURL
                 }
             }
  4. In the lower left Sidebar, click Run or press Control-R/Command-R to run the app. ::: labs-info Run the app from within ArcGIS AppStudio by clicking the app in the App Gallery and then clicking the Run icon from the tool pallette.

    In Qt Creator, there are several ways to run the project:

    • Press Control-R/Command-R on the keyboard.
    • In the app menu, click Build > Run.
    • Click the Run button from the tool palette. :::

Add the Trails and Parks and Open Space feature layers

  1. In the App declaration, add URL properties for the Trails (lines) and Parks and Open Space (polygons) feature layers. ::: labs-info Map layers in the operationalLayers list model are drawn in the order they are added (first layer to the last layer in the collection). Discover other ways to control the layer ordering in the API reference documentation for LayerListModel.

    To ensure the features layers are rendered in the correct order, polygons layers are generally added first, then lines, and then points. :::

    Use dark colors for code blocksCopy
         
    1
    2
    3
    4
    5
    property string trailheadsURL: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"
    
    // *** ADD ***
    property string trailsURL: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0"
    property string parksURL: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0"
  2. In the Map declaration, add code to add the two layers. Be sure to add them in the correct drawing order: parks, trails, trailheads.

    Use dark colors for code blocksCopy
                    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
          Map {
            id:map
    
            BasemapTopographicVector {}
    
            // *** ADD ***
            FeatureLayer {
                ServiceFeatureTable {
                    url: parksURL
                }
            }
            FeatureLayer {
                ServiceFeatureTable {
                    url: trailsURL
                }
            }
  3. Run your code to view the map. You should see all three layers rendered in the correct order.

::: labs-panel Note: The hosted feature layers do not have styles or pop-ups defined for them on the server-side, so feature layers use default symbols to draw them in the map. To learn how to improve the visualization of the feature layers, try the Configure layers tutorial to apply styles on the server-side. :::

Congratulations, you're done!

Your map should load and center on the Santa Monica Mountains with a feature layer that shows the trailheads as orange markers on the map, the trail lines in purple, and the parks and open spaces polygons in blue. Compare your map with our completed solution project.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.