SP GlassKit1.1.0

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.

GlassMapControls running in SP Glass Studio.Capture
Capture of the component running in SP Glass Studio on the iOS 26.5 simulator. Liquid Glass is rendered by the system; this is an image, not a live preview.

Installation

spglass add glass-map-controls

Copies the sources and their dependencies into your project. Set up the CLI.

Swift
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")

import SPGlassKit
Identifierglass-map-controls
ModuleSPGlassMaps
Kindcustom-glass — a GlassGroup: the two zoom buttons share one glass pill split by a hairline, extra controls keep their own surfaces
PlatformsiOS 26, iPadOS 26, macOS 26
FrameworksNone. MapKit is optional (GlassMapKitEngine, compiled only where MapKit can be imported)
ExampleExamples/Gallery/Maps/GlassMapControlsExample.swift

Minimal usage

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

Variants

VariantUse
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: .horizontalA row instead of a column.

Parameters

ParameterTypeDefaultMeaning
canZoomInBooltrueFalse at the closest zoom: the button is disabled.
canZoomOutBooltrueFalse at the farthest zoom.
axisAxis.verticalDirection of the cluster.
sizeGlassControlSize.regularSize of the zoom buttons.
onZoomIn, onZoomOut@MainActor () -> Void—Called on activation.
extra@ViewBuildernoneControls placed before the zoom buttons.
enginesome 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.

Example

Complete example (compiled with the package)
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

Identifierglass-map-controls
Version1.1.0
Depends onglass-compass, glass-foundation, glass-icon-button, glass-maps-support
FrameworksSwiftUI
Public symbolsGlassMapControls
LicenceMIT
Manifestmanifest.json

Files