Add a feature layer from an item
Learn how to use a ID to access and display a feature layer in a map.
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 item IDs to access the Trailheads, Trails, and Parks and Open Spaces feature layers and display in them in a map.
The following are required for this tutorial:
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
Each portal item has a unique
itemID, identified from its URL on ArcGIS Online.
The feature layer referenced with this portal item contains trailheads in and around the Santa Monica Mountains.
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")
AGSFeatureLayerobject from the portal item and a layer ID. Add the layer to the map's operational layers.
A layer ID is required because a portal item may have more than one layer. For instance, when a feature service has 3 layers - trailheads (points), trails (polylines) and trail areas (polygons), the corresponding portal item would contain all three 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.
Other ways to run the project in Xcode:
- In Xcode's app menu, select Product > Run.
- Pressing the Run button at the top-left of the Xcode project window.
Your app should display a map with the trailheads centered on the Santa Monica Mountains. Compare your solution with our completed solution project.