Show a legend for all the operational layers in the map.
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
- Open the sample
- Scroll through the legends to see the various elements that represent features on the map.
How it works
- Layers implement the
AGSLayerContent
interface, which provides an API for gettingAGSLegendInfo
objects.AGSLegendInfo
contains anAGSSymbol
and a name string.AGSLayerContent.fetchLegendInfos(completion:)
must be called on eachAGSLayerContent
instance to fetch the info from the data. - For each symbol in the
AGSLegendInfo
array,AGSSymbol.createSwatch(completion:)
must be called so that an image of theAGSSymbol
is returned. - 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
// 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
}
}