LegendControl

The LegendControl acts as a high-level manager for map legends within a Mapbox environment.

It coordinates the lifecycle, visual state, and unit synchronization (Metric/Imperial) of legends. This class handles:

  • Shared Legends: Uses reference counting to ensure a single legend ID can be used by multiple layers, only removing the UI when the last layer is removed.

  • Unit Conversion: Supports dynamic toggling between Imperial and Metric units across all hosted legends.

  • UI Management: Automatically manages a LegendContainerView, including the injection of separators and specialized LegendView instances.

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class Coordinator(provider: () -> LegendControl.Host?)

A lifecycle utility that facilitates the "Try Install" pattern between a Host and a LegendControl.

Link copied to clipboard
interface Host

Defines a contract for a UI component (usually an Activity or Fragment) capable of hosting a LegendControl.

Properties

Link copied to clipboard

The background color of the legend container.

Link copied to clipboard
Link copied to clipboard

Controls the visibility of the legend container. The container should also be hidden if there are no legends to display.

Link copied to clipboard

The currently active legends, mapped by their unique ID. This list should be observed by the UI to render the correct legends.

Link copied to clipboard
var mapView: MapView?
Link copied to clipboard

Max height (dp) of each hosted LegendView's scrollable content area. Host apps can mirror this when sizing the outer getView, or adjust independently via layout params.

Link copied to clipboard

Invoked when the user toggles units via toggleUnits (legend tap). The map host should set this to com.xweather.mapsgl.map.MapController.units so data-query *-text labels reformat with the same system.

Link copied to clipboard

Indicates whether tapping the legend view toggles between metric and imperial units. This state should be used by the UI to add a click handler.

Link copied to clipboard

Units applied to the hosted legends. Updating this value propagates the new units to each registered legend.

Link copied to clipboard

When true, new and updated legends use light text on a dark panel (see setDarkTheme).

Functions

Link copied to clipboard
fun add(legend: Legend)

Adds a legend to the container.

Link copied to clipboard

Applies units to all hosted legends. Used by the map host to keep legends aligned with com.xweather.mapsgl.map.MapController.units, and by toggleUnits when no host callback is set.

Link copied to clipboard

Retrieves the currently active Legend instance for the given identifier.

Link copied to clipboard

Returns the LegendContainerView associated with this control. This view acts as the visual parent for all individual legend items.

Link copied to clipboard
fun removeLegend(id: String, force: Boolean = false)

Removes a legend it from the UI if no longer needed.

Link copied to clipboard
fun replaceLegend(id: String, legend: Legend)

Replaces a registered legend in the cache and refreshes its view. Use when applying host-side presentation overrides that must survive toggleUnits.

Link copied to clipboard
fun setDarkTheme(enabled: Boolean = true)

Switches the legend panel to a dark map-overlay style (semi-transparent black panel, white labels). Re-applies presentation to legends already on the control.

Link copied to clipboard

Default title color for legends added while the container host color is in effect.

Link copied to clipboard

Toggles the measurement units between METRIC and IMPERIAL if toggleUnitsOnTap is true. This function is intended to be called by the UI (e.g., from a click listener).

Link copied to clipboard
fun update(legend: Legend)

Updates the data of an existing legend without changing its position in the UI.