GlassMapControls
Zoom and orientation actions in a glass container, independent of the map provider.
- Custom glass
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-map-controls
Draws its own Liquid Glass surface, with an opaque form when Reduce Transparency is on.
CaptureInstallation
spglass add glass-map-controlsCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| 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
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
@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:
@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
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/canZoomOutit 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 callsGlassMapControls(engine:)there). GlassMapKitEnginezooms by halving or doubling the camera distance insidedistanceRange; it does not animate by itself — wrap the action inwithAnimationin the host if wanted. Its behaviour against a liveMapwas not exercised in a running application during verification; the camera arithmetic is unit-tested.
Example
import SwiftUI
import SPGlassKit
#if canImport(MapKit)
import MapKit
#endif
public struct GlassMapControlsExample: GlassExample {
public static let componentID = "glass-map-controls"
@State private var engine = SampleMapEngine()
#if canImport(MapKit)
@State private var usesMapKit = false
@State private var mapKitEngine = GlassMapKitEngine(
position: .camera(MapCamera(centerCoordinate: CLLocationCoordinate2D(latitude: 45.4215, longitude: -75.6972), distance: 6_000, heading: 30))
)
#endif
public init() {}
public var body: some View {
VStack(spacing: 12) {
#if canImport(MapKit)
Toggle("Use the MapKit adapter (loads map tiles)", isOn: $usesMapKit)
.padding(.horizontal)
if usesMapKit {
mapKitMap
} else {
placeholderMap
}
#else
placeholderMap
#endif
}
}
/// A provider-independent engine: the controls only see `GlassMapEngine`.
private var placeholderMap: some View {
SampleMapBackdrop(heading: engine.heading ?? 0, zoom: engine.zoomScale)
.overlay(alignment: .topTrailing) {
GlassMapControls(engine: engine) {
GlassCompass(heading: engine.heading) { engine.resetNorth() }
GlassIconButton(systemImage: "rotate.right", accessibilityLabel: "Rotate the sample map") {
engine.rotate(by: 45)
}
}
.padding()
}
.overlay(alignment: .bottom) {
GlassMapControls(canZoomIn: engine.canZoomIn, canZoomOut: engine.canZoomOut, axis: .horizontal, size: .small) {
engine.zoomIn()
} onZoomOut: {
engine.zoomOut()
}
.padding(.bottom, 40)
}
}
#if canImport(MapKit)
private var mapKitMap: some View {
Map(position: $mapKitEngine.position)
.onMapCameraChange(frequency: .continuous) { context in
mapKitEngine.cameraChanged(context.camera)
}
.overlay(alignment: .topTrailing) {
GlassMapControls(engine: mapKitEngine)
.padding()
}
}
#endif
}
#Preview("GlassMapControls") { GlassMapControlsExample().glassTheme(.ocean) }
Registry entry
| Identifier | glass-map-controls |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-compass, glass-foundation, glass-icon-button, glass-maps-support |
| Frameworks | SwiftUI |
| Public symbols | GlassMapControls |
| Licence | MIT |
| Manifest | manifest.json |
Files
Maps/Maps/GlassMapControls.swift7298 bytes · sha256 10270a0439f7…