Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassMaps/Maps/GlassMapKitEngine.swift8// Module : SPGlassMaps9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041#if canImport(MapKit)42import SwiftUI43import MapKit44import SPGlassCore4546/// Optional MapKit adapter: drives a SwiftUI `Map` through its camera position.47///48/// ```swift49/// @State private var engine = GlassMapKitEngine()50///51/// Map(position: $engine.position)52/// .onMapCameraChange(frequency: .continuous) { engine.cameraChanged($0.camera) }53/// .overlay(alignment: .topTrailing) { GlassMapControls(engine: engine).padding() }54/// ```55///56/// The adapter keeps the last camera reported by the map and writes a new ``position`` for each action. It57/// never requests the location permission: ``centerOnUser()`` asks MapKit to follow the user, which only has58/// an effect once the host application has obtained the authorisation.59///60/// This file is the only one in `SPGlassMaps` that imports MapKit; the other components compile without it.61@MainActor62@Observable63public final class GlassMapKitEngine: GlassMapEngine {64 /// Bind this to `Map(position:)`.65 public var position: MapCameraPosition66 /// The last camera reported through ``cameraChanged(_:)``; `nil` until the map reports one.67 public private(set) var camera: MapCamera?68 /// Closest and farthest camera distances, in metres.69 public var distanceRange: ClosedRange<Double>7071 public init(position: MapCameraPosition = .automatic, distanceRange: ClosedRange<Double> = 150...20_000_000) {72 self.position = position73 self.distanceRange = distanceRange74 camera = position.camera75 }7677 /// Call from `onMapCameraChange` so that the controls follow gestures made on the map.78 public func cameraChanged(_ camera: MapCamera) {79 self.camera = camera80 }8182 public var heading: Double? {83 camera.map { GlassHeading.normalized($0.heading) }84 }8586 public var canZoomIn: Bool {87 camera.map { GlassMapZoom.canZoomIn(distance: $0.distance, in: distanceRange) } ?? false88 }8990 public var canZoomOut: Bool {91 camera.map { GlassMapZoom.canZoomOut(distance: $0.distance, in: distanceRange) } ?? false92 }9394 public func zoomIn() { zoom(.in) }9596 public func zoomOut() { zoom(.out) }9798 public func resetNorth() {99 guard let camera else { return }100 apply(MapCamera(centerCoordinate: camera.centerCoordinate, distance: camera.distance, heading: 0, pitch: camera.pitch))101 }102103 public func centerOnUser() {104 position = .userLocation(fallback: position)105 }106107 private func zoom(_ direction: GlassMapZoom.Direction) {108 guard let camera else { return }109 let distance = GlassMapZoom.distance(after: direction, from: camera.distance, in: distanceRange)110 apply(MapCamera(centerCoordinate: camera.centerCoordinate, distance: distance, heading: camera.heading, pitch: camera.pitch))111 }112113 private func apply(_ camera: MapCamera) {114 self.camera = camera115 position = .camera(camera)116 }117}118#endif119