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 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.
Make sure you have installed the latest version of Xcode.
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
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")
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)
Press Command-R to run the app in the iOS Simulator.
Your app should display a map with the trailheads centered on the Santa Monica Mountains. Compare your solution with our completed solution project.
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.
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
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.
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.