Display a web map with a point feature layer that has feature reduction enabled to aggregate points into clusters.
Map displaying the feature layer with feature reduction property enabled by default:
Popup message displaying the cluster details:
Use case
Feature clustering can be used to dynamically aggregate groups of points that are within proximity of each other in order to represent each group with a single symbol. Such grouping allows you to see patterns in the data that are difficult to visualize when a layer contains hundreds or thousands of points that overlap and cover each other.
How to use the sample
Pan and zoom the map to view how clustering is dynamically updated. Disable clustering to view the original point features that make up the clustered elements. When clustering is On, you can click on a clustered geoelement to view aggregated information and summary statistics for that cluster. When clustering is toggled off and you click on the original feature you get access to information about individual power plant features.
How it works
- Create a map from a web map
PortalItem
. - Get the cluster enabled layer from the map's operational layers.
- Get the
FeatureReduction
from the feature layer and setisEnabled
value to enable or disable clustering on the feature layer. - When the user clicks on the map, call
identifyLayers
and pass in the map's screen coordinates. - Get the
Popup
and the correspondingPopupElement
from the resultingIdentifyLayerResult
and use it to construct a popup output string. - Use
Html.fromHtml
to convert the html tags in the output string to a styled text and display it to the user.
Relevant API
- FeatureLayer
- FeatureReduction
- IdentifyLayerResult
- Popup
- PopupElement
- PopupField
- Portal
- PortalItem
About the data
This sample uses a web map that displays the Esri Global Power Plants feature layer with feature reduction enabled. When enabled, the aggregate features symbology shows the color of the most common power plant type, and a size relative to the average plant capacity of the cluster.
Additional information
This sample uses the GeoViewCompose Toolkit module to be able to implement a Composable MapView.
Tags
aggregate, bin, cluster, geoviewcompose, group, merge, normalize, reduce, summarize, toolkit
Sample Code
/* Copyright 2023 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.displayclusters
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
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.displayclusters.screens.MainScreen
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)
setContent {
SampleAppTheme {
FeatureReductionApp()
}
}
}
@Composable
private fun FeatureReductionApp() {
Surface(
color = MaterialTheme.colorScheme.background
) {
MainScreen(
sampleName = getString(R.string.display_clusters_app_name)
)
}
}
}