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 API keys. If you don't have an account, sign up for free.
Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.
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 Android Studio: in the Android tool window, open app > java > com.example.app > MainActivity.
Among the imports you are pasting into your app is one from the Android API: import android.graphics.Color. You will need this class when setting colors for the symbols.
Steps
Add UI for selecting a predefined query expression
To make performing a query on a feature layer more flexible, add a Spinner control that presents a list of predefined attribute queries for the dataset from the LA County Parcels feature layer.
In Android Studio, in the Android tool window, open app > res > values > strings.xml.
Add XML that specifies text to use as a label for the spinner. Then provide the strings that the spinner will present to the user. These are the SQL expressions for querying the feature layer.
Add a new ConstraintLayout inside the existing ConstraintLayout. In the new constraint layout, define a TextView for the spinner label, and then define the Spinner.
Add code to execute the selected query for the current map extent
In this step, create a new function that queries a FeatureLayer (identified using its ID) 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 FeatureQueryResult will be selected in the parcels layer.
The function takes three arguments: the ID for the layer to query (string), a SQL expression that defines attribute criteria (string), and the area of the MapView currently being viewed (Envelope).
Create a queryFeatureLayer() function takes three parameters. Access the map's operational layers, and use layerId to retrieve the feature layer you wish to query.
Then get the layer's feature table.
Clear any previous selections from the feature layer. Then create query parameters, using the whereExpression and queryExtent parameters passed into the function.
Call queryFeaturesAsync(), passing in the query parameters. The call returns a listenable future that will contain the feature query result. Add a done listener, which will execute when the query is complete.
Create the Parcels feature layer and add a selection listener to the spinner
Create the LA County Parcels FeatureLayer from the feature service. Providing a Layer.id allows for referencing the Parcels layer from the layer collection when needed.
Next, create the spinner that was defined in activity_main.xml, and add an OnItemSelectedListener to the spinner.
Then add the parcels feature layer to map's collection of data layers (GeoModel.getOperationalLayers()) and finally, define the selection color via the map view's SelectionProperties to distinguish selected features in the map.
Create a new function named createFeatureLayer() and pass in the map. Create a service feature table from a feature service URL. Then add an id property for use when querying the layer.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Create the parcels feature layer. When it is loaded, add a listener on the spinner.// Add the layer to the map, and set the map view's selection properties to yellow.privatefuncreateFeatureLayer(map: ArcGISMap) {
val serviceFeatureTable =
ServiceFeatureTable("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0")
val parcelsFeatureLayer = FeatureLayer(serviceFeatureTable)
// give the layer an ID so we can easily find it later, then add it to the map parcelsFeatureLayer.id = "Parcels" }
Expand
Load the feature layer and add a done loading listener. In the listener, declare local variable spinner and bind it to the spinner you defined in activity_main.xml. Then create the spinner using ArrayAdapter.createFromResource().
Inside the layer's done loading listener, initialize the spinner's onItemSelectedListener property with an object expression that implements the interface AdapterView.OnItemSelectedListener. Implement onNothing() with an empty function body. Implement onItemSelected() so it gets the current extent and the current selection in the spinner and then calls queryFeatureLayer() with layer Id "Parcels", the spinner choice, and the extent.
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.