-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMapView.swift
More file actions
234 lines (189 loc) · 8.82 KB
/
Copy pathNativeMapView.swift
File metadata and controls
234 lines (189 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import PDFKit
import SwiftUI
import UIKit
struct NativeMapView: View {
@State private var selectedMapStation: Station? = nil
var body: some View {
NavigationStack {
GeometryReader { geometry in
ZoomableMapScrollView(
viewSize: geometry.size,
selectedStation: $selectedMapStation
)
}
.ignoresSafeArea(edges: .bottom)
.navigationTitle("System Map")
.navigationBarTitleDisplayMode(.inline)
.navigationDestination(item: $selectedMapStation) { station in
StationDetailView(station: station)
}
}
}
}
#if DEBUG
class DebugRegionsOverlayView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
isUserInteractionEnabled = false // Let gestures pass through to parent
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.saveGState()
for region in MapRegions.all {
let cgPath = region.path.cgPath
context.addPath(cgPath)
// Fill: semi-transparent red
context.setFillColor(UIColor.red.withAlphaComponent(0.2).cgColor)
// Stroke: solid red outline
context.setStrokeColor(UIColor.red.cgColor)
context.setLineWidth(1.5)
context.drawPath(using: .fillStroke)
// Draw station code at the center of the bounding box
let bounds = cgPath.boundingBox
let text = region.id.replacingOccurrences(of: "#", with: "")
let attrs: [NSAttributedString.Key: Any] = [
.font: UIFont.boldSystemFont(ofSize: 9),
.foregroundColor: UIColor.systemBlue,
]
let stringSize = text.size(withAttributes: attrs)
let textRect = CGRect(
x: bounds.midX - stringSize.width / 2,
y: bounds.midY - stringSize.height / 2,
width: stringSize.width,
height: stringSize.height
)
// White text backdrop for contrast
context.setFillColor(UIColor.white.withAlphaComponent(0.75).cgColor)
context.fill(textRect)
text.draw(in: textRect, withAttributes: attrs)
}
context.restoreGState()
}
}
#endif
struct ZoomableMapScrollView: UIViewRepresentable {
let viewSize: CGSize
@Binding var selectedStation: Station?
func renderPDF(document: PDFDocument, scale: CGFloat = 3.0) -> UIImage? {
guard let page = document.page(at: 0), let pageRef = page.pageRef else { return nil }
let targetSize = CGSize(width: 2000, height: 1718)
let pixelWidth = Int(targetSize.width * scale)
let pixelHeight = Int(targetSize.height * scale)
let format = UIGraphicsImageRendererFormat()
format.scale = 1.0 // Render explicitly at pixel coordinates
let renderer = UIGraphicsImageRenderer(size: CGSize(width: pixelWidth, height: pixelHeight), format: format)
return renderer.image { context in
let cgContext = context.cgContext
// Fill background with white
cgContext.setFillColor(UIColor.white.cgColor)
cgContext.fill(CGRect(x: 0, y: 0, width: pixelWidth, height: pixelHeight))
// Flip coordinates for Y-up PDF rendering
cgContext.translateBy(x: 0, y: CGFloat(pixelHeight))
cgContext.scaleBy(x: 1.0, y: -1.0)
// Scale to match target size (2000x1718) and resolution multiplier
cgContext.scaleBy(x: 1.92 * scale, y: 1.92 * scale)
// Translate to the bottom-left of the crop box in PDF points
cgContext.translateBy(x: -16.6667, y: -133.875)
cgContext.drawPDFPage(pageRef)
}
}
func makeUIView(context: Context) -> UIScrollView {
let scrollView = UIScrollView()
scrollView.delegate = context.coordinator
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.bouncesZoom = true
scrollView.decelerationRate = .fast
let pdfURL = Bundle.main.url(forResource: "system-map-rail", withExtension: "pdf")
if let url = pdfURL, let document = PDFDocument(url: url) {
let size = CGSize(width: 2000, height: 1718)
// Render the PDF to a high-res image (3x scale) in memory
if let image = renderPDF(document: document, scale: 3.0) {
let imageView = UIImageView(image: image)
imageView.frame = CGRect(origin: .zero, size: size)
imageView.accessibilityIdentifier = "dc_metro_silver.png"
imageView.isUserInteractionEnabled = true
#if DEBUG
let debugOverlay = DebugRegionsOverlayView(frame: CGRect(origin: .zero, size: size))
imageView.addSubview(debugOverlay)
#endif
let tapGesture = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleTap(_:)))
imageView.addGestureRecognizer(tapGesture)
scrollView.addSubview(imageView)
scrollView.contentSize = size
context.coordinator.imageView = imageView
}
}
return scrollView
}
func updateUIView(_ uiView: UIScrollView, context: Context) {
context.coordinator.parent = self
guard context.coordinator.imageView != nil else { return }
let contentSize = CGSize(width: 2000, height: 1718)
// Use viewSize provided by GeometryReader
if viewSize.height > 0, context.coordinator.lastHeight != viewSize.height {
context.coordinator.lastHeight = viewSize.height
// Calculate minimum scale to fit height
let heightScale = viewSize.height / contentSize.height
uiView.minimumZoomScale = heightScale
uiView.maximumZoomScale = 5.0
// Set initial scale
uiView.zoomScale = heightScale
// Center horizontally if the content is wider than the screen
let contentWidth = contentSize.width * heightScale
if contentWidth > viewSize.width {
let offsetX = (contentWidth - viewSize.width) / 2
uiView.contentOffset = CGPoint(x: offsetX, y: 0)
}
// Call scrollViewDidZoom to ensure initial centering logic fires
context.coordinator.scrollViewDidZoom(uiView)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UIScrollViewDelegate {
var parent: ZoomableMapScrollView
weak var imageView: UIImageView?
var lastHeight: CGFloat = 0
init(_ parent: ZoomableMapScrollView) {
self.parent = parent
}
func viewForZooming(in _: UIScrollView) -> UIView? {
return imageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
guard let imageView = imageView else { return }
// Keeps the image perfectly centered if zoomed out smaller than the scroll view
let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
imageView.center = CGPoint(
x: scrollView.contentSize.width * 0.5 + offsetX,
y: scrollView.contentSize.height * 0.5 + offsetY
)
}
@objc func handleTap(_ gesture: UITapGestureRecognizer) {
guard let imageView = imageView else { return }
// Tap coordinates in the 2000x1718 coordinate space!
let location = gesture.location(in: imageView)
for region in MapRegions.all {
if region.path.contains(location) {
let code = region.id.replacingOccurrences(of: "#", with: "")
let regionCodes = code.components(separatedBy: ",")
if let station = Station.allStations.first(where: { station in
let stationCodes = station.id.components(separatedBy: ",")
return !Set(regionCodes).isDisjoint(with: Set(stationCodes))
}) {
parent.selectedStation = station
return
}
}
}
}
}
}