Repository Public MIT
1# GlassMapControls23| | |4|---|---|5| Identifier | `glass-map-controls` |6| Module | `SPGlassMaps` |7| Kind | `custom-glass` — a `GlassGroup`: the two zoom buttons share one glass pill split by a hairline, extra controls keep their own surfaces |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Frameworks | None. MapKit is optional (`GlassMapKitEngine`, compiled only where MapKit can be imported) |10| Example | `Examples/Gallery/Maps/GlassMapControlsExample.swift` |1112## Minimal usage1314```swift15GlassMapControls {16 zoom += 117} onZoomOut: {18 zoom -= 119}20```2122## Variants2324| Variant | Use |25|---|---|26| `init(onZoomIn:onZoomOut:)` | Zoom buttons only, driven by closures. |27| `init(…extra:)` | Extra controls before the zoom buttons: a `GlassCompass`, a `GlassLocationButton`, any `GlassIconButton`. |28| `init(engine:)` | Zoom buttons and a compass wired to a `GlassMapEngine`. |29| `init(engine:extra:)` | Zoom buttons wired to an engine, with your own extra controls. |30| `axis: .horizontal` | A row instead of a column. |3132## Parameters3334| Parameter | Type | Default | Meaning |35|---|---|---|---|36| `canZoomIn` | `Bool` | `true` | False at the closest zoom: the button is disabled. |37| `canZoomOut` | `Bool` | `true` | False at the farthest zoom. |38| `axis` | `Axis` | `.vertical` | Direction of the cluster. |39| `size` | `GlassControlSize` | `.regular` | Size of the zoom buttons. |40| `onZoomIn`, `onZoomOut` | `@MainActor () -> Void` | — | Called on activation. |41| `extra` | `@ViewBuilder` | none | Controls placed before the zoom buttons. |42| `engine` | `some GlassMapEngine` | — | Source of `canZoomIn`, `canZoomOut`, `heading` and of the actions. |4344### The engine protocol4546```swift47@MainActor48public protocol GlassMapEngine: AnyObject, Observable {49 var heading: Double? { get }50 var canZoomIn: Bool { get }51 var canZoomOut: Bool { get }52 func zoomIn()53 func zoomOut()54 func resetNorth()55 func centerOnUser()56}57```5859A 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.6061### The optional MapKit adapter6263`GlassMapKitEngine` (file `GlassMapKitEngine.swift`, inside `#if canImport(MapKit)`) drives a SwiftUI `Map` through `MapCameraPosition`:6465```swift66@State private var engine = GlassMapKitEngine()6768Map(position: $engine.position)69 .onMapCameraChange(frequency: .continuous) { engine.cameraChanged($0.camera) }70 .overlay(alignment: .topTrailing) { GlassMapControls(engine: engine).padding() }71```7273## States7475Idle, 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.7677## Advanced example7879```swift80GlassMapControls(engine: engine) {81 GlassCompass(heading: engine.heading) { withAnimation { engine.resetNorth() } }82 GlassLocationButton(state: location.state) { location.buttonTapped() }83 GlassIconButton(systemImage: "square.3.layers.3d", accessibilityLabel: "Map layers") { showsLayers = true }84}85.padding()86```8788## Accessibility8990- The cluster is a container labelled "Map controls"; each button keeps its own label ("Zoom in", "Zoom out") and tooltip.91- Hit targets are at least 44 points on iOS and iPadOS.92- With Reduce Transparency the group is a plain container and the buttons use the system's own opaque treatment.9394## Limits9596- The component does not know the map: disabled states are only as accurate as the `canZoomIn` / `canZoomOut` it is given.97- 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).98- `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.99