Learn how to use a URL 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 URLs to access the Trailheads, Trails, and Parks and Open Spaces feature layers and display them in a map.
Prerequisites
The following are required for this tutorial:
- An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
- Your system meets the system requirements.
Steps
Open the Xcode project
-
To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
-
Open the
.xcodeproj
file in Xcode. -
If you downloaded the solution project, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
-
Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
- In Xcode, in the Project Navigator, click AppDelegate.swift.
- In the editor, set the
APIKey
property on theAGSArcGISRuntime
with your API key.Environment
AppDelegate.swiftUse dark colors for code blocks func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Note: it is not best practice to store API keys in source code. // The API key is referenced here for the convenience of this tutorial. AGSArcGISRuntimeEnvironment.apiKey = "YOUR_API_KEY" return true }
-
Add a feature layer to the map
-
In the project navigator, open the ViewController.swift file. Update the
setup
method to create anMap() AGSService
to access the trail heads data and anFeature Table AGSFeature
to view it. Add the layer to the map's operational layers.Layer -
An
AGSService
is effectively an in-memory database of the features from a service URL.Feature Table -
An
AGSFeature
andLayer AGSService
work together to request, cache locally, and display features on a map.Feature Table -
Operational layers are map layers that users can interact with and appear overlaid on top of the basemap.
-
The trail heads feature service was created in advance for this tutorial. Review the Style a feature layer tutorial to learn how.
ViewController.swiftUse dark colors for code blocks private func setupMap() { let map = AGSMap(basemapStyle: .arcGISTopographic) let featureLayer: AGSFeatureLayer = { let featureServiceURL = URL(string: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0")! let trailheadsTable = AGSServiceFeatureTable(url: featureServiceURL) return AGSFeatureLayer(featureTable: trailheadsTable) }() map.operationalLayers.add(featureLayer) mapView.map = map mapView.setViewpoint( AGSViewpoint( latitude: 34.09042, longitude: -118.71511, scale: 200_000 ) ) }
-
-
Press Command + R to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Big Sur 11.3, Xcode 13, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see point, line, and polygon features (representing trailheads, trails, and parks) draw on the map for an area in the Santa Monica Mountains.
What's Next?
Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: