Show popup

View on GitHub

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 a Map instance from a PortalItem of a web map.
  2. Create a MapView with the Map.
  3. Use the GeoViewProxy.identifyLayers(_:screenPoint:tolerance:returnPopupsOnly:maximumResultsPerLayer:) method to identify the top-most feature.
  4. Create a PopupView with the result's first popup.

Relevant API

  • IdentifyLayerResult
  • Map
  • PopupView

About the data

This sample uses a web map that displays reported incidents in San Francisco.

Tags

feature, feature layer, popup, web map

Sample Code

ShowPopupView.swift
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
// 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
//
//   https://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.

import SwiftUI
import ArcGIS
import ArcGISToolkit

struct ShowPopupView: View {
    /// A map of reported incidents in San Francisco.
    @State private var map = Map(
        item: PortalItem(
            portal: .arcGISOnline(connection: .anonymous),
            id: .incidentsInSanFrancisco
        )
    )

    /// The screen point to perform an identify operation.
    @State private var identifyScreenPoint: CGPoint?

    /// The popup to be shown as the result of the layer identify operation.
    @State private var popup: Popup?

    /// A Boolean value specifying whether the popup view should be shown or not.
    @State private var showPopup = false

    var body: some View {
        MapViewReader { proxy in
            MapView(map: map)
                .onSingleTapGesture { screenPoint, _ in
                    identifyScreenPoint = screenPoint
                }
                .task(id: identifyScreenPoint) {
                    guard let identifyScreenPoint = identifyScreenPoint,
                          let identifyResult = try? await proxy.identifyLayers(
                            screenPoint: identifyScreenPoint,
                            tolerance: 12,
                            returnPopupsOnly: false
                          )
                    else { return }
                    self.popup = identifyResult.first?.popups.first
                    self.showPopup = self.popup != nil
                }
                .floatingPanel(
                    selectedDetent: .constant(.full),
                    horizontalAlignment: .leading,
                    isPresented: $showPopup
                ) { [popup] in
                    PopupView(popup: popup!, isPresented: $showPopup)
                        .showCloseButton(true)
                        .padding()
                }
        }
    }
}

private extension PortalItem.ID {
    /// The ID used in the "Incidents in San Francisco" portal item.
    static var incidentsInSanFrancisco: Self { Self("fb788308ea2e4d8682b9c05ef641f273")! }
}

#Preview {
    ShowPopupView()
}

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