Overview

You will learn: how to load a layer from an ArcGIS Online item and add it to a map.

The ArcGIS Runtime SDK for iOS allows you to access items (such as layers) stored in ArcGIS Online by their item identifier. Feature layers can store geographic features (points, lines, polygons), edit or update attributes, and synchronize changes. As demonstrated in the lab Display a web map, you can load and display an existing web map that was preconfigured with a basemap and layers. Alternatively, you may wish to load one or more layers from ArcGIS Online and create a map with code. You may do this if you have operational or base layers from different sources in your app. You can publish your own feature layer to ArcGIS Online using the Create a new dataset data lab.

In this lab you will learn how to access a layer stored in ArcGIS Online. You will open the layer using its identifier and display its contents on a map.

Before you begin

Make a copy of the finished Create a starter app lab (or download here) and open the .xcodeproj file in Xcode.

Steps

  1. In the ViewController.swift file, create an AGSPortal object and an AGSPortalItem object referencing the feature layer.

     override func viewDidLoad() {
         super.viewDidLoad()
    
         /*** ADD ***/
         let portal = AGSPortal(url: URL(string: "https://www.arcgis.com")!, loginRequired: false)
         let item = AGSPortalItem(portal: portal, itemID: "883cedb8c9fe4524b64d47666ed234a7")
         /*** ADD ***/
    
         let map = AGSMap(basemapType: .navigationVector, latitude: 34.05293, longitude: -118.24368, levelOfDetail: 9)
         mapView.map = map
     }
    
  2. Create an AGSFeatureLayer object from the portal item and a layer id. Add the layer to the map's operational layers.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let portal = AGSPortal(url: URL(string: "https://www.arcgis.com")!, loginRequired: false)
        let item = AGSPortalItem(portal: portal, itemID: "883cedb8c9fe4524b64d47666ed234a7")
        let map = AGSMap(basemapType: .navigationVector, latitude: 34.05293, longitude: -118.24368, levelOfDetail: 9)
    
        /*** ADD ***/
        let layer = AGSFeatureLayer(item: item, layerID: 0)
        map.operationalLayers.add(layer)
        /*** ADD ***/
    
        mapView.map = map
    }
    
  3. Press Command-R to run the app in the iOS Simulator.

Congratulations, you're done!

Your app should display a map with the trail heads centered on the Los Angeles area.

Your project should look like this.

Challenge

Use your portal item

Instead of using the portal item we used above, use the one you made in the data lab Create a new dataset.

In Step 1 above, replace the itemID parameter with the id of your portal item.

Load a portal item using its entire URL

Instead of creating a portal and then a portal item like we did in the lab, create just a portal item with its entire URL using the following initializer available in the SDK. You don't need to create an AGSPortal object.

let itemUrl = "https://www.arcgis.com/home/item.html?id=883cedb8c9fe4524b64d47666ed234a7"
let item = AGSPortalItem(url: URL(string: itemUrl))!

As before, create a layer from the portal item and add it to the map.

Discover data

If you have not already done so, visit the Discover data lab and search for interesting open data you could add in another layer in your app.