Add a feature layer

Learn how to use a URL to access and display a feature layer in a map.

add a feature layer

A map contains layers of geographic data. A map contains a basemap layer and, optionally, one or more data layers. This tutorial shows you how to access and display a feature layer in a map. You access feature layers with an item ID or URL. You will use URLs to access the Trailheads, Trails, and Parks and Open Spaces feature layers and display them in a map.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.

Steps

Open the Xcode project

  1. To start the tutorial, complete the Display a map tutorial or download and unzip the solution.

  2. Open the .xcodeproj file in Xcode.

  3. If you downloaded the solution project, set your API key.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    1. 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.

    2. In Xcode, in the Project Navigator, click MainApp.swift.
    3. In the Editor, set the ArcGISEnvironment.apiKey property on the ArcGISEnvironment with your API key.

    MainApp.swift
    Expand
    Use dark colors for code blocks
                                        
    Change lineChange lineChange line
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    import SwiftUI
    
    import ArcGIS
    
    @main
    struct MainApp: App {
    
        init() {
            ArcGISEnvironment.apiKey = APIKey("<#your-API-key#>")
        }
    
        var body: some SwiftUI.Scene {
            WindowGroup {
                ContentView()
    
                    .ignoresSafeArea()
    
            }
        }
    
    }

Add a feature layer to the map

  1. In the project navigator, open the ContentView.swift file. Update the map object. Create a ServiceFeatureTable to access the trail heads data and a FeatureLayer to view it. Add the layer to the map's operational layers.

    ContentView.swift
    Use dark colors for code blocks
                                             
    Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
        @StateObject private var map: Map = {
            let map = Map(basemapStyle: .arcGISTopographic)
    
            map.initialViewpoint = Viewpoint(latitude: 34.02700, longitude: -118.80500, scale: 72_000)
    
            let featureServiceURL = URL(string: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0")!
            let serviceFeatureTable = ServiceFeatureTable(url: featureServiceURL)
            let trailheadsLayer = FeatureLayer(featureTable: serviceFeatureTable)
            map.addOperationalLayer(trailheadsLayer)
    
            return map
        }()
    
  2. Press <Command+R> to run the app.

    If you are using the Xcode simulator your system must meet these minimum requirements: macOS Big Sur 11.3, Xcode 13.4, iOS 14.0. If you are using a physical device, then refer to the system requirements.

You should see point, line, and polygon features (representing trailheads, trails, and parks) draw on the map for an area in the Santa Monica Mountains.

What's Next?

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