View on GitHub Sample viewer app

Query features on a map using an Arcade expression.

QueryFeaturesWithArcadeExpression

Use case

Arcade is a portable, lightweight, and secure expression language used to create custom content in ArcGIS applications. Like other expression languages, it can perform mathematical calculations, manipulate text, and evaluate logical statements. It also supports multi-statement expressions, variables, and flow control statements. What makes Arcade particularly unique when compared to other expression and scripting languages is its inclusion of feature and geometry data types. This sample uses an Arcade expression to query the number of crimes in a neighborhood in the last 60 days.

How to use the sample

Tap on any neighborhood to see the number of crimes in the last 60 days in a TextView.

How it works

  1. Create a PortalItem using the URL and ID.

  2. Create an ArcGISMap using the portal item.

  3. Create a MapViewProxy to handle user interaction with the map view.

  4. Provide behaviour for the MapView’s onSingleTapConfirmed parameter to react to taps on the map.

  5. Identify the visible layer where it is tapped using mapViewProxy.identify() and get the feature from the result.

  6. Create the following ArcadeExpression:

    expressionValue = "var crimes = FeatureSetByName(\$map, 'Crime in the last 60 days');\n" +
    "return Count(Intersects(\$feature, crimes));"
  7. Create an ArcadeEvaluator using the Arcade expression and ArcadeProfile.FormCalculation.

  8. Create a map of profile variables with the following key-value pairs:

    mapOf<String, Any>("\$feature" to feature, "\$map" to mapView.map)
  9. Call ArcadeEvaluator.evaluate() on the Arcade evaluator object and pass the profile variables map.

  10. Get the ArcadeEvaluationResult.result.

  11. Convert the result to a numerical value (Double) and pass it to the UI.

Relevant API

  • ArcadeEvaluationResult
  • ArcadeEvaluator
  • ArcadeExpression
  • ArcadeProfile
  • Portal
  • PortalItem

About the data

This sample uses the Crimes in Police Beats Sample ArcGIS Online Web Map which contains 2 layers for city beats borders, and crimes in the last 60 days as recorded by the Rochester, NY police department.

Additional information

This sample uses the GeoView-Compose module of the ArcGIS Maps SDK for Kotlin Toolkit to implement a Composable MapView.

Visit Getting Started on the ArcGIS Developer website to learn more about Arcade expressions.

Tags

Arcade evaluator, Arcade expression, geoview-compose, identify layers, portal, portal item, query, toolkit

Sample Code

MainActivity.kt MainActivity.kt QueryFeaturesWithArcadeExpressionViewModel.kt QueryFeaturesWithArcadeExpressionScreen.kt
/* Copyright 2024 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.arcgismaps.sample.queryfeatureswitharcadeexpression
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
import com.esri.arcgismaps.sample.queryfeatureswitharcadeexpression.screens.QueryFeaturesWithArcadeExpressionScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// authentication with an API key or named user is
// required to access basemaps and other location services
ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
enableEdgeToEdge()
setContent {
SampleAppTheme {
QueryFeaturesWithArcadeExpressionApp()
}
}
}
@Composable
private fun QueryFeaturesWithArcadeExpressionApp() {
Surface(color = MaterialTheme.colorScheme.background) {
QueryFeaturesWithArcadeExpressionScreen(
sampleName = getString(R.string.query_features_with_arcade_expression_app_name)
)
}
}
}