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. To do so, you need to access each item by ID. You can find an item's ID by browsing to the item on ArcGIS Online and identifying it at the end of the url, e.g. https://www.arcgis.com/home/item.html?id=fbca9c87feb94ba5b00411b3a00809f3. You can also 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.

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 2e4b3df6ba4b44969a3bc9827de746b3.

Steps

  1. Open the ViewController.swift file, update the setupMap() method to create an AGSPortal object and an AGSPortalItem object referencing the feature layer.

         mapView.map = AGSMap(basemapType: .navigationVector, latitude: 34.09042, longitude: -118.71511, levelOfDetail: 10)
    
         // *** ADD ***
         let portal = AGSPortal(url: URL(string: "https://www.arcgis.com")!, loginRequired: false)
         let item = AGSPortalItem(portal: portal, itemID: "2e4b3df6ba4b44969a3bc9827de746b3")
    
  2. Create an AGSFeatureLayer object from the portal item and a layer ID. Add the layer to the map's operational layers.

         let item = AGSPortalItem(portal: portal, itemID: "2e4b3df6ba4b44969a3bc9827de746b3")
    
         // *** ADD ***
         let layer = AGSFeatureLayer(item: item, layerID: 0)
         mapView.map!.operationalLayers.add(layer)
    
  3. Press Command-R to run the app in the iOS Simulator.

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

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=2e4b3df6ba4b44969a3bc9827de746b3"
    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.