SPB Git

Repository Public MIT

SampleMapBackdrop.swift 4.5 KB 105 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Examples/Gallery/Maps/SampleMapBackdrop.swift8// Module           : Examples9// 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// =============================================================================4041import SwiftUI42import SPGlassKit4344/// A drawn stand-in for a map, used by the map examples so that they work offline and without MapKit.45/// It is not a map: a grid of "streets" that rotates and scales with the injected heading and zoom.46struct SampleMapBackdrop: View {47    var heading: Double = 048    var zoom: Double = 14950    var body: some View {51        Canvas { context, size in52            context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(Color(red: 0.86, green: 0.9, blue: 0.84)))53            let center = CGPoint(x: size.width / 2, y: size.height / 2)54            context.translateBy(x: center.x, y: center.y)55            context.rotate(by: .degrees(-heading))56            let step = 56 * zoom57            let reach = max(size.width, size.height)58            var streets = Path()59            var offset = -reach60            while offset <= reach {61                streets.move(to: CGPoint(x: offset, y: -reach))62                streets.addLine(to: CGPoint(x: offset, y: reach))63                streets.move(to: CGPoint(x: -reach, y: offset))64                streets.addLine(to: CGPoint(x: reach, y: offset))65                offset += step66            }67            context.stroke(streets, with: .color(.white), lineWidth: 6 * min(zoom, 2))68            let park = CGRect(x: step * 0.15, y: -step * 1.85, width: step * 1.7, height: step * 0.7)69            context.fill(Path(roundedRect: park, cornerRadius: 6), with: .color(Color(red: 0.62, green: 0.8, blue: 0.6)))70            let water = CGRect(x: -step * 2.85, y: step * 1.15, width: step * 1.7, height: step * 1.7)71            context.fill(Path(ellipseIn: water), with: .color(Color(red: 0.6, green: 0.78, blue: 0.92)))72        }73        .overlay(alignment: .bottomLeading) {74            Text("Sample data · drawn placeholder, not a real map")75                .font(.caption2)76                .padding(6)77                .background(.background.opacity(0.8), in: RoundedRectangle(cornerRadius: 6))78                .padding(8)79        }80        .accessibilityElement()81        .accessibilityLabel("Sample map placeholder")82        .accessibilityAddTraits(.isImage)83    }84}8586/// A map engine for the examples: a proprietary engine would look like this, without MapKit.87@MainActor88@Observable89final class SampleMapEngine: GlassMapEngine {90    private(set) var heading: Double? = 4091    private(set) var zoomLevel = 292    private(set) var isFollowingUser = false93    let zoomLevels = 0...49495    var canZoomIn: Bool { zoomLevel < zoomLevels.upperBound }96    var canZoomOut: Bool { zoomLevel > zoomLevels.lowerBound }97    var zoomScale: Double { pow(1.5, Double(zoomLevel - 2)) }9899    func zoomIn() { if canZoomIn { zoomLevel += 1 } }100    func zoomOut() { if canZoomOut { zoomLevel -= 1 } }101    func resetNorth() { heading = 0 }102    func centerOnUser() { isFollowingUser.toggle() }103    func rotate(by degrees: Double) { heading = GlassHeading.normalized((heading ?? 0) + degrees) }104}105
Files main
Menu

Theme