Learn how to execute a SQL query to return features from a feature layer based on spatial and attribute criteria.
A feature layer can contain a large number of features stored in ArcGIS. You can query a layer to access a subset of its features using any combination of spatial and attribute criteria. You can control whether or not each feature's geometry is returned, as well as which attributes are included in the results. Queries allow you to return a well-defined subset of your hosted data for analysis or display in your ArcGIS Runtime app.
In this tutorial, you'll write code to perform SQL queries that return a subset of features in the LA County Parcel feature layer (containing over 2.4 million features). Features that meet the query criteria are selected in the 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.
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
Set selection properties
Define the selection color via the map view's AGSSelectionProperties to distinguish selected features in the map view.
In Xcode, in the Project Navigator, click ViewController.swift.
Update the setupMap() method to set map view's selection color to be yellow. This step is optional, and if you don't set a selection color, cyan is used as the default.
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.
The app loads with the map centered on the Santa Monica Mountains in California with the parcels feature layer displayed.
Create a UI control to display predefined query strings
To make performing a query on a feature layer flexible, add a UI control to present a list of predefined attribute queries for the parcels dataset.
Next, add layout constraints to define the position of the button relative to the main view and safeArea of the device. This ensures the button looks as expected in different devices and orientations.
Next, add a target for the button with the method to execute when it is tapped. This method, populateOptions(), which you'll create in the next section, populates and displays a list of query options.
Create an UIAlertController with a title and of style actionSheet. Create an array of strings to populate the where clauses. Create an alert action for each where clause, which calls the method to execute the query when chosen. You'll implement the method executeQuery(whereClause:) later.
In this step, create a new method that queries the parcels AGSFeatureLayer using both attribute and spatial criteria. After clearing any currently selected features, a new query will be executed to find features in the map's current extent that meet the selected attribute expression. The features in the AGSFeatureQueryResult will be selected in the parcels layer.
Add the executeQuery function to query parcels using a provided where expression.
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.
The app loads with the map centered on the Santa Monica Mountains in California with the parcels feature layer displayed. Choose an attribute expression, and parcels in the current extent that meet the selected criteria will display in the specified selection color.