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.

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 tutorial Display a web map, you can load and display an existing web map that was pre-configured 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 tutorial.

In this tutorial 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 sure you have installed the latest version of Xcode.

Reuse the starter project

If you have completed the Create a starter app tutorial, then copy the project into a new empty folder. Otherwise, download and unzip the project solution. Open the .xcodeproj file in Xcode. Run and verify the map displays in the device simulator.

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 tutorial 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 tutorial, 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 tutorial and search for interesting open data you could add in another layer in your app.