Repository Public MIT
1import SwiftUI2import SPGlassKit3#if canImport(MapKit)4import MapKit5#endif67public struct GlassMapControlsExample: GlassExample {8 public static let componentID = "glass-map-controls"9 @State private var engine = SampleMapEngine()10 #if canImport(MapKit)11 @State private var usesMapKit = false12 @State private var mapKitEngine = GlassMapKitEngine(13 position: .camera(MapCamera(centerCoordinate: CLLocationCoordinate2D(latitude: 45.4215, longitude: -75.6972), distance: 6_000, heading: 30))14 )15 #endif1617 public init() {}1819 public var body: some View {20 VStack(spacing: 12) {21 #if canImport(MapKit)22 Toggle("Use the MapKit adapter (loads map tiles)", isOn: $usesMapKit)23 .padding(.horizontal)24 if usesMapKit {25 mapKitMap26 } else {27 placeholderMap28 }29 #else30 placeholderMap31 #endif32 }33 }3435 /// A provider-independent engine: the controls only see `GlassMapEngine`.36 private var placeholderMap: some View {37 SampleMapBackdrop(heading: engine.heading ?? 0, zoom: engine.zoomScale)38 .overlay(alignment: .topTrailing) {39 GlassMapControls(engine: engine) {40 GlassCompass(heading: engine.heading) { engine.resetNorth() }41 GlassIconButton(systemImage: "rotate.right", accessibilityLabel: "Rotate the sample map") {42 engine.rotate(by: 45)43 }44 }45 .padding()46 }47 .overlay(alignment: .bottom) {48 GlassMapControls(canZoomIn: engine.canZoomIn, canZoomOut: engine.canZoomOut, axis: .horizontal, size: .small) {49 engine.zoomIn()50 } onZoomOut: {51 engine.zoomOut()52 }53 .padding(.bottom, 40)54 }55 }5657 #if canImport(MapKit)58 private var mapKitMap: some View {59 Map(position: $mapKitEngine.position)60 .onMapCameraChange(frequency: .continuous) { context in61 mapKitEngine.cameraChanged(context.camera)62 }63 .overlay(alignment: .topTrailing) {64 GlassMapControls(engine: mapKitEngine)65 .padding()66 }67 }68 #endif69}7071#Preview("GlassMapControls") { GlassMapControlsExample().glassTheme(.ocean) }72