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 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.
For each symbol in the AGSLegendInfo array, AGSSymbol.createSwatch(completion:) must be called so that an image of the AGSSymbol is returned.
The names and images are then displayed next to each other in a list.
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
classMILShowLegendViewController: UIViewController, UIAdaptivePresentationControllerDelegate{
@IBOutletprivateweakvar mapView: AGSMapView!
@IBOutletprivateweakvar legendBBI: UIBarButtonItem!
privatevar map: AGSMap!
overridefuncviewDidLoad() {
super.viewDidLoad()
// add the source code button item to the right of navigation bar (self.navigationItem.rightBarButtonItem as!SourceCodeBarButtonItem).filenames = ["MILShowLegendViewController", "MILLegendTableViewController"]
// initialize the mapself.map =AGSMap(basemapStyle: .arcGISTopographic)
// create a map image layer using a urllet mapImageLayer =AGSArcGISMapImageLayer(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer")!)
// add the image layer to the mapself.map.operationalLayers.add(mapImageLayer)
// create feature table using a urllet featureTable =AGSServiceFeatureTable(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0")!)
// create feature layer using this feature tablelet featureLayer =AGSFeatureLayer(featureTable: featureTable)
// add feature layer to the mapself.map.operationalLayers.add(featureLayer)
self.map.load { [weakself] (error: Error?) inif error ==nil {
self?.legendBBI.isEnabled =true }
}
self.mapView.map =self.map
// zoom to a custom viewpointself.mapView.setViewpointCenter(AGSPoint(x: -11e6, y: 6e6, spatialReference: .webMercator()), scale: 9e7)
}
// MARK: - Navigationoverridefuncprepare(forsegue: 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: - UIAdaptivePresentationControllerDelegatefuncadaptivePresentationStyle(forcontroller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
returnUIModalPresentationStyle.none
}
}