SPB Git

Repository Public MIT

doc-glass-map-controls.md 4.1 KB

# GlassMapControls

Identifier glass-map-controls
Module SPGlassMaps
Kind custom-glass — a GlassGroup: the two zoom buttons share one glass pill split by a hairline, extra controls keep their own surfaces
Platforms iOS 26, iPadOS 26, macOS 26
Frameworks None. MapKit is optional (GlassMapKitEngine, compiled only where MapKit can be imported)
Example Examples/Gallery/Maps/GlassMapControlsExample.swift

# Minimal usage

swift
GlassMapControls {
    zoom += 1
} onZoomOut: {
    zoom -= 1
}

# Variants

Variant Use
init(onZoomIn:onZoomOut:) Zoom buttons only, driven by closures.
init(…extra:) Extra controls before the zoom buttons: a GlassCompass, a GlassLocationButton, any GlassIconButton.
init(engine:) Zoom buttons and a compass wired to a GlassMapEngine.
init(engine:extra:) Zoom buttons wired to an engine, with your own extra controls.
axis: .horizontal A row instead of a column.

# Parameters

Parameter Type Default Meaning
canZoomIn Bool true False at the closest zoom: the button is disabled.
canZoomOut Bool true False at the farthest zoom.
axis Axis .vertical Direction of the cluster.
size GlassControlSize .regular Size of the zoom buttons.
onZoomIn, onZoomOut @MainActor () -> Void — Called on activation.
extra @ViewBuilder none Controls placed before the zoom buttons.
engine some GlassMapEngine — Source of canZoomIn, canZoomOut, heading and of the actions.

# The engine protocol

swift
@MainActor
public protocol GlassMapEngine: AnyObject, Observable {
    var heading: Double? { get }
    var canZoomIn: Bool { get }
    var canZoomOut: Bool { get }
    func zoomIn()
    func zoomOut()
    func resetNorth()
    func centerOnUser()
}

A proprietary map engine conforms to it with an @Observable class; nothing in the protocol refers to MapKit. GlassMapZoom offers the arithmetic for engines that zoom by camera distance, GlassHeading the heading arithmetic.

# The optional MapKit adapter

GlassMapKitEngine (file GlassMapKitEngine.swift, inside #if canImport(MapKit)) drives a SwiftUI Map through MapCameraPosition:

swift
@State private var engine = GlassMapKitEngine()

Map(position: $engine.position)
    .onMapCameraChange(frequency: .continuous) { engine.cameraChanged($0.camera) }
    .overlay(alignment: .topTrailing) { GlassMapControls(engine: engine).padding() }

# States

Idle, pressed and focused are drawn by the system glass button style. Each zoom button is disabled at its limit. With an engine that has not reported a camera yet (GlassMapKitEngine before the first cameraChanged), both buttons are disabled.

# Advanced example

swift
GlassMapControls(engine: engine) {
    GlassCompass(heading: engine.heading) { withAnimation { engine.resetNorth() } }
    GlassLocationButton(state: location.state) { location.buttonTapped() }
    GlassIconButton(systemImage: "square.3.layers.3d", accessibilityLabel: "Map layers") { showsLayers = true }
}
.padding()

# Accessibility

  • The cluster is a container labelled "Map controls"; each button keeps its own label ("Zoom in", "Zoom out") and tooltip.
  • Hit targets are at least 44 points on iOS and iPadOS.
  • With Reduce Transparency the group is a plain container and the buttons use the system's own opaque treatment.

# Limits

  • The component does not know the map: disabled states are only as accurate as the canZoomIn / canZoomOut it is given.
  • The engine's properties are read when the parent view builds the controls; the parent must therefore read the engine in its body (it does when it calls GlassMapControls(engine:) there).
  • GlassMapKitEngine zooms by halving or doubling the camera distance inside distanceRange; it does not animate by itself — wrap the action in withAnimation in the host if wanted. Its behaviour against a live Map was not exercised in a running application during verification; the camera arithmetic is unit-tested.
Files main
Menu

Theme