Show legend

View on GitHubSample viewer app

Show a legend for all the operational layers in the map.

Image of Show legend 1 Image of Show legend 2

Use case

Legends are used to describe what each symbol on a map represents. A common format is to show an image of the symbol alongside of a text description of what that symbol represents. This sample demonstrates how to show a legend for all the operational layers in the map.

How to use the sample

  1. Open the sample
  2. Scroll through the legends to see the various elements that represent features on the map.

How it works

  1. Layers implement the AGSLayerContent interface, which provides an API for getting AGSLegendInfo objects. AGSLegendInfo contains an AGSSymbol and a name string. AGSLayerContent.fetchLegendInfos(completion:) must be called on each AGSLayerContent instance to fetch the info from the data.
  2. For each symbol in the AGSLegendInfo array, AGSSymbol.createSwatch(completion:) must be called so that an image of the AGSSymbol is returned.
  3. The names and images are then displayed next to each other in a list.

Relevant API

  • AGSLayerContent
  • AGSLegendInfo

Tags

legend, legend info, symbol swatch, toolkit

Sample Code

MILShowLegendViewController.swiftMILShowLegendViewController.swiftMILLegendTableViewController.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
// Copyright 2016 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 MILShowLegendViewController: UIViewController, UIAdaptivePresentationControllerDelegate {
    @IBOutlet private weak var mapView: AGSMapView!
    @IBOutlet private weak var legendBBI: UIBarButtonItem!

    private var map: AGSMap!

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the source code button item to the right of navigation bar
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["MILShowLegendViewController", "MILLegendTableViewController"]

        // initialize the map
        self.map = AGSMap(basemapStyle: .arcGISTopographic)

        // create a map image layer using a url
        let mapImageLayer = AGSArcGISMapImageLayer(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer")!)
        // add the image layer to the map
        self.map.operationalLayers.add(mapImageLayer)

        // create feature table using a url
        let featureTable = AGSServiceFeatureTable(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0")!)
        // create feature layer using this feature table
        let featureLayer = AGSFeatureLayer(featureTable: featureTable)
        // add feature layer to the map
        self.map.operationalLayers.add(featureLayer)

        self.map.load { [weak self] (error: Error?) in
            if error == nil {
                self?.legendBBI.isEnabled = true
            }
        }

        self.mapView.map = self.map

        // zoom to a custom viewpoint
        self.mapView.setViewpointCenter(AGSPoint(x: -11e6, y: 6e6, spatialReference: .webMercator()), scale: 9e7)
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "LegendTableSegue" {
            let controller = segue.destination as! MILLegendTableViewController
            controller.presentationController?.delegate = self
            controller.preferredContentSize = CGSize(width: 300, height: 200)
            controller.operationalLayers = self.map.operationalLayers
        }
    }

    // MARK: - UIAdaptivePresentationControllerDelegate

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }
}

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