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 AGSMap using a URL.
  2. Set the map to an AGSMapView and set the touchDelegate.
  3. Use the AGSGeoView.identifyLayer(_:screenPoint:tolerance:returnPopupsOnly:completion:) method to identify the top-most feature.
  4. Create an AGSPopupsViewController with the result's popups.
  5. Present the view controller.

Relevant API

  • AGSIdentifyLayerResult
  • AGSMap
  • AGSPopupsViewController

About the data

This sample uses a feature layer that displays reported incidents in San Francisco.

Tags

feature, feature layer, popup, web map

Sample Code

ShowPopupViewController.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
75
76
77
78
79
80
81
82
83
84
// 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.

import UIKit
import ArcGIS

class ShowPopupViewController: UIViewController, AGSGeoViewTouchDelegate, AGSPopupsViewControllerDelegate {
    @IBOutlet weak var mapView: AGSMapView! {
        didSet {
            mapView.map = makeMap()
        }
    }

    var featureLayer: AGSFeatureLayer?

    func makeMap() -> AGSMap {
        // Create a map using a URL.
        let mapURL = URL(string: "https://arcgisruntime.maps.arcgis.com/home/item.html?id=fb788308ea2e4d8682b9c05ef641f273")!
        let map = AGSMap(url: mapURL)!
        // Load the map.
        map.load { [weak self] (error) in
            guard let self = self else { return }
            if let error = error {
                self.presentAlert(error: error)
            } else {
                // Set touch delegate.
                self.mapView.touchDelegate = self
                // Get the feature layer.
                self.featureLayer = map.operationalLayers.firstObject as? AGSFeatureLayer
            }
        }
        return map
    }

    // MARK: - AGSGeoViewTouchDelegate
    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        guard let featureLayer = self.featureLayer else { return }
        // Identify the specified feature layer.
        mapView.identifyLayer(featureLayer, screenPoint: screenPoint, tolerance: 12, returnPopupsOnly: false) { [weak self] (result: AGSIdentifyLayerResult) in
            guard let self = self else { return }
            if let error = result.error {
                self.presentAlert(error: error)
            } else if !result.popups.isEmpty {
                // Unselect the previous feature.
                featureLayer.clearSelection()
                // Select the new feature.
                let features = result.geoElements as? [AGSFeature]
                let selectedFeature = features?.first
                featureLayer.select(selectedFeature!)
                // Display a popup only if it exists.
                let popupsViewController = AGSPopupsViewController(popups: result.popups)
                // Display the popup as a formsheet -- specified for iPads.
                popupsViewController.modalPresentationStyle = .formSheet
                // Present the popup.
                popupsViewController.delegate = self
                self.present(popupsViewController, animated: true)
            }
        }
    }

    // MARK: - AGSPopupsViewControllerDelegate methods
    func popupsViewControllerDidFinishViewingPopups(_ popupsViewController: AGSPopupsViewController) {
        // Dismiss the popups view controller.
        dismiss(animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Add the source code button item to the right of navigation bar.
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["ShowPopupViewController"]
    }
}

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