Controls
Legend

Legend Control

A legend control is responsible for managing the collection of legends currently visible within the context of an interface or map and provides the interface needed for adding and removing legends.

Legend control with multiple layers
// Initialize a legend control
let legendControl = LegendControl()
 
// Adds a legend control to the map.
controller.add(legendControl: legendControl)
 
// Removes the legend control
controller.removeLegendControl()

Refer to the LegendControl API reference for more information about the available properties and methods.

Legend Presentation

Once a LegendControl has been configured and added to the MapController, it is the client’s responsibility to place the view appropriately within the view hierarchy.

The presentation method will vary depending on the UI framework, as shown below. For best results, fix the view’s width to the desired layout and allow its height to adjust automatically based on its intrinsic content size.

LegendControlView wraps the LegendControl's view allowing the legend view to be placed within a SwiftUI view hierarchy.

The LegendControlView will use an existing LegendControl if one has been added to the underlying MapController. This allows the client to customize the LegendControl and add custom legends if necessary.

struct ContentView : View {
    // Map controller state initialized manually elsewhere in the app.
    @State private var mapController: LegendControl.Host
 
    var body: some View {
        ZStack {
            Group {
                // Map content...
            }
            // Place the legend on top of the map, in the bottom right corner.
            VStack {
                Spacer()
                HStack {
                    Spacer()
                    LegendControlView(mapControllerProvider: { mapController })
                        .frame(maxWidth: 300)
                        .padding()
                }
            }
        }
    }
}