Learn how to apply renderers and label definitions to a feature layer based on attribute values.
Applications can display feature layer data with different styles to enhance the visualization. The type of AGSRenderer you choose depends on your application. An AGSSimpleRenderer applies the same symbol to all features, an AGSUniqueValueRenderer applies a different symbol to each unique attribute value, and an AGSClassBreaksRenderer applies a symbol to a range of numeric values. Renderers are responsible for accessing the data and applying the appropriate symbol to each feature when the layer draws. You can also use a AGSLabelDefinition to show attribute information for features. Visit the Styles and data visualization documentation to learn more about styling layers.
You can also author, style and save web maps, web scenes, and layers as portal items and then add them to the map in your application. Visit the following tutorials to learn more about adding portal items.
In this tutorial, you will apply different renderers to enhance the visualization of three feature layers with data for the Santa Monica Mountains: Trailheads with a single symbol, Trails based on elevation change and bike use, and Parks and Open Spaces based on the type of park.
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.
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 the AGSArcGISRuntimeEnvironment with your API key.
AppDelegate.swift
Expand
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
funcapplication(_application: UIApplication,
didFinishLaunchingWithOptionslaunchOptions: [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"returntrue }
Expand
Add app configuration constants
Create a configuration file to specify constants that can be used by the app to connect to data and resources.
Add a new swift file named AppConfiguration.swift.
In Xcode's app menu, select File > New > File....
Select Swift File from the iOS tab's Source sub-menu.
Name the file AppConfiguration and ensure your app's target is checked.
Click Create.
Create four static URLs: three for accessing feature layers, and a fourth for accessing a static image for use in a picture marker symbol. You will use these resources in future steps.
Create a method to apply a different symbol for each type of park area to the Parks and Open Spaces feature layer.
Add a new method named addOpenSpaceLayer() in ViewController.swift.
AGSUniqueValue assigns a symbol to a value or values. A unique value renderer uses a collection of unique values to assign the appropriate symbol for each feature it renderers.
For this example, the renderer uses a feature's TYPE attribute value to apply the correct symbol.
When the app opens, Parks and Open Spaces feature layer is added to the map. The map displays the different types of parks and open spaces with four unique symbols.
Add a layer with a class breaks renderer
Create a method to apply a different symbol for each of the five ranges of elevation gain to the Trails feature layer.
Add a new method named addTrailsLayer().
An AGSClassBreak assigns a symbol to a range of values.
For this example, the renderer uses each feature's ELEV_GAIN attribute value to classify it into a defined range (class break) and apply the corresponding symbol.
When the app opens, the Trails feature layer is added to the map. The map displays trails with different symbols depending on trail elevation.
Add layers with definition expressions
You can use a definition expression to define a subset of features to display.
Features that do not meet the expression criteria are not displayed by the layer. In the following steps, you will create two methods that use a definition expression to apply a symbol to a subset of features in the Trails feature layer.
AGSFeatureLayer.DefinitionExpression uses a SQL expression to limit the features available for query and display. Your code will create two layers that each display a different subset of trails based on the value for the USE_BIKE field. Trails that allow bikes will be symbolized with a blue symbol ("USE_BIKE = 'Yes'") and those that don't will be red ("USE_BIKE = 'No'"). Another way to symbolize these features would be to create a AGSUniqueValueRenderer that applies a different symbol for these values.
Add a method with a definition expression to filter for trails that permit bikes.
When the app opens, two Trails feature layers are added to the map. One shows where bikes are permitted and the other where they are prohibited.
Add a layer with a label definition
Create a method to style trailheads with hiker images and labels for the Trailheads feature layer.
Create a helper method named makeLabelDefinition() to define a label defintion based on a specific attribute of the feature layer TRL_NAME. Also define label placement and symbol.
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.
When the app opens, all the layers you've created and symbolized are displayed on the map.
Parks and open spaces are displayed with four unique symbols
Trails use different symbols (line widths) depending on trail elevation
Trails are blue where bikes are permitted and red where they are prohibited
Trailheads are displayed with a hiker icon and labels display each trail's name