Show popup

View on GitHubSample viewer app

Show a predefined popup from a web map.

Show popup screenshot

Use case

Many web maps contain predefined popups which are used to display the attributes associated with each feature layer in the map, such as hiking trails, land values, or unemployment rates. You can display text, attachments, images, charts, and web links. Rather than creating new popups to display information, you can easily access and display the predefined popups.

How to use the sample

Tap on the features to prompt a popup that displays information about the feature.

How it works

  1. Create and load an ArcGISMap using a URL.
  2. Create a MapViewProxy and set it to the composable MapView.
  3. Use the mapViewProxy.identify(...) function to identify the top-most feature.
  4. From the identified feature get the Popup.
  5. Pass the Popup to the composable Popup to display it.

Relevant API

  • ArcGISMap
  • IdentifyLayerResult
  • Popup

About the data

This sample uses a feature layer that displays reported information about mountains in the Sierra Nevada.

Additional information

This sample uses the Popup Toolkit module to offer an out of the box solution for displaying popup information in the UI. This sample also uses the GeoView-Compose Toolkit module to be able to implement a composable MapView.

Tags

feature, feature layer, popup, toolkit, web map

Sample Code

ShowPopupViewModel.ktShowPopupViewModel.ktMainActivity.ktShowPopupScreen.kt
Use dark colors for code blocksCopy
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
/* Copyright 2025 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.showpopup.components

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.unit.dp
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.data.Feature
import com.arcgismaps.geometry.GeometryType
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.PortalItem
import com.arcgismaps.mapping.layers.FeatureLayer
import com.arcgismaps.mapping.popup.Popup
import com.arcgismaps.mapping.view.SingleTapConfirmedEvent
import com.arcgismaps.portal.Portal
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch

class ShowPopupViewModel(application: Application) : AndroidViewModel(application) {

    val mapViewProxy = MapViewProxy()

    // Create a portal and load a map with information about mountains in the Sierra Nevada
    private val portal = Portal("https://arcgis.com/")
    private val portalItem = PortalItem(portal, "9f3a674e998f461580006e626611f9ad")
    val arcGISMap = ArcGISMap(portalItem)

    // Get the first visible feature layer with a popup definition
    private val featureLayer: FeatureLayer
        get() {
            return arcGISMap.operationalLayers.filterIsInstance<FeatureLayer>().first { featureLayer ->
                (featureLayer.featureTable?.geometryType == GeometryType.Point)
                    .and(featureLayer.isVisible)
                    .and(featureLayer.isPopupEnabled)
                    .and(featureLayer.popupDefinition != null)
            }
        }

    // Popup that gets passed to the main screen composable
    var popup: Popup? by mutableStateOf(null)
        private set

    // Keep track of the identified feature
    private var identifiedFeature: Feature? = null

    // Create a message dialog view model for handling error messages
    val messageDialogVM = MessageDialogViewModel()

    init {
        viewModelScope.launch {
            arcGISMap.load().onFailure { error ->
                messageDialogVM.showMessageDialog(
                    title = "Failed to load map",
                    description = error.message.toString()
                )
            }
        }
    }

    /**
     * Identify the feature at the given screen coordinate, select the feature, and show the popup for it.
     */
    fun identifyForPopup(singleTapConfirmedEvent: SingleTapConfirmedEvent) {
        viewModelScope.launch {
            mapViewProxy.identify(
                layer = featureLayer,
                screenCoordinate = singleTapConfirmedEvent.screenCoordinate,
                tolerance = 12.dp,
                returnPopupsOnly = true
            ).onSuccess { result ->
                popup = result.popups.first().also { popup ->
                    identifiedFeature = (popup.geoElement as Feature).also { identifiedFeature ->
                        featureLayer.selectFeature(identifiedFeature)
                    }
                }
            }.onFailure { error ->
                messageDialogVM.showMessageDialog(
                    title = "Failed to identify: ${error.message}",
                    description = error.message.toString()
                )
            }
        }
    }

    /**
     * Dismiss the popup and unselect the identified feature.
     */
    fun onDismissRequest() {
        popup = null
        identifiedFeature?.let { featureLayer.unselectFeature(it) }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.