Limit the features displayed on a map with a definition expression.
Use case
Set a definition expression to filter out the features to be displayed. You might filter a dataset of tree quality selecting for only those trees which require maintenance or are damaged.
How to use the sample
Tap the 'Apply Expression' button to limit the features requested from the feature layer to those specified by the SQL query definition expression. Tap the 'Reset' button to remove the definition expression on the feature layer, which returns all the records.
How it works
Create a ServiceFeatureTable from a URL.
Create a FeatureLayer from the service feature table.
Set the definition expression of the features on your feature layer using the setDefinitionExpression().
Relevant API
FeatureLayer
FeatureLayer.setDefinitionExpression
ServiceFeatureTable
About the data
This map displays point features related to crime incidents that have been reported by city residents.
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
/*
* Copyright 2020 Esri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/package com.esri.arcgisruntime.sample.featurelayerdefinitionexpression
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.data.ServiceFeatureTable
import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.geometry.SpatialReferences
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.featurelayerdefinitionexpression.databinding.ActivityMainBinding
classMainActivity : AppCompatActivity() {
// set flag for applying expression to feature layerprivatevar applyExpression: Boolean = trueprivateval activityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
privateval mapView: MapView by lazy {
activityMainBinding.mapView
}
privateval applyExpressionButton: Button by lazy {
activityMainBinding.applyExpressionButton
}
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activityMainBinding.root)
// authentication with an API key or named user is required to access basemaps and other// location services ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)
// create a service feature table from a URLval serviceFeatureTable = ServiceFeatureTable(getString(R.string.sample_service_url))
// create a feature layer using the service feature tableval featureLayer = FeatureLayer(serviceFeatureTable)
// create a map with a topographic base mapval map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC).apply {
// add the feature layer to the map operationalLayers.add(featureLayer)
}
mapView.apply {
// set the map to be displayed in the layout's map viewthis.map = map
// set a viewpoint on the map view to center on San Francisco setViewpointCenterAsync(
Point(-13630845.0, 4544861.0, SpatialReferences.getWebMercator()), 200000.0 )
}
// set button listener applyExpressionButton.setOnClickListener {
if (applyExpression) {
// set the definition expression to nothing (empty string, null also works) featureLayer.definitionExpression = ""// change the text to apply applyExpressionButton.text = getString(R.string.action_def_exp)
applyExpression = false } else {
// apply a definition expression on the feature layer// if this is called before the layer is loaded, it will be applied to the loaded layer featureLayer.definitionExpression = "req_Type = 'Tree Maintenance or Damage'"// change the text to reset applyExpressionButton.text = getString(R.string.action_reset)
applyExpression = true }
}
// click to set initial state applyExpressionButton.performClick()
}
overridefunonResume() {
super.onResume()
mapView.resume()
}
overridefunonPause() {
mapView.pause()
super.onPause()
}
overridefunonDestroy() {
mapView.dispose()
super.onDestroy()
}
}