Skip to content

Commit 0e9dd89

Browse files
author
Achyut Kumar M
committed
replace annotations with visible rect
1 parent d4e97d9 commit 0e9dd89

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

GoInfoGame/GoInfoGame/UI/Map/CustomMap.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ struct CustomMap: UIViewRepresentable {
3737
@Binding var useBingMaps: Bool
3838

3939
@Binding var tappedCoordinate: CLLocationCoordinate2D?
40+
41+
var shadowOverlay: ShadowOverlay
4042

4143
// Creates and configures the UIView
4244
func makeUIView(context: Context) -> MKMapView {
@@ -84,13 +86,9 @@ struct CustomMap: UIViewRepresentable {
8486

8587
// Remove existing overlays
8688
mapView.overlays.forEach { mapView.removeOverlay($0) }
87-
88-
// Add shadow overlay with rectangular cutouts
89-
if mapView.overlays.first(where: { $0 is ShadowOverlay }) == nil {
90-
let overlay = ShadowOverlay(annotations: mapView.annotations)
91-
mapView.addOverlay(overlay)
92-
}
93-
89+
90+
mapView.addOverlay(shadowOverlay)
91+
9492

9593
// Re-add overlays based on selection
9694
if useBingMaps {
@@ -126,7 +124,7 @@ struct CustomMap: UIViewRepresentable {
126124

127125
// Creates the coordinator
128126
func makeCoordinator() -> Coordinator {
129-
Coordinator(self)
127+
Coordinator(self, shadowOverlay: shadowOverlay)
130128
}
131129

132130
// Coordinator class for managing delegate methods
@@ -139,12 +137,15 @@ struct CustomMap: UIViewRepresentable {
139137
var currentAnnotation: DisplayUnitAnnotation?
140138
var mapView: MKMapView?
141139

140+
let shadowOverlay: ShadowOverlay
141+
142142
private let maxZoomAltitude: CLLocationDistance = 100
143143
private var zoomReachedLimit: Bool = false
144144

145-
init(_ parent: CustomMap) {
145+
init(_ parent: CustomMap, shadowOverlay: ShadowOverlay) {
146146
self.parent = parent
147147
self.contextualInfo = parent.contextualInfo
148+
self.shadowOverlay = shadowOverlay
148149
}
149150

150151
@objc func handleMapTap(_ gestureRecognizer: UITapGestureRecognizer) {
@@ -181,6 +182,16 @@ struct CustomMap: UIViewRepresentable {
181182
mapView.setRegion(newRegion, animated: true)
182183
}
183184

185+
func mapViewDidFinishRenderingMap(_ mapView: MKMapView, fullyRendered: Bool) {
186+
guard fullyRendered else { return }
187+
188+
if shadowOverlay.visibleRects.isEmpty {
189+
shadowOverlay.addVisibleRect(mapView.visibleMapRect)
190+
mapView.removeOverlay(shadowOverlay)
191+
mapView.addOverlay(shadowOverlay)
192+
}
193+
}
194+
184195
//renders polyline
185196
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
186197
if let routePolyline = overlay as? MKPolyline {

GoInfoGame/GoInfoGame/UI/Map/MapView.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct MapView: View {
5151
@State private var showManageQuestSheet = false
5252

5353
@State private var showZoomInAlert = false
54+
55+
@State private var shadowOverlay = ShadowOverlay()
5456

5557
var body: some View {
5658
NavigationStack {
@@ -79,7 +81,7 @@ struct MapView: View {
7981
print(contextualInfo)
8082
selectedDetent = .fraction(0.8)
8183
self.setContextualInfo(contextualinfo: contextualInfo)
82-
}, useBingMaps: $useBingMaps, tappedCoordinate: $tappedCoordinate)
84+
}, useBingMaps: $useBingMaps, tappedCoordinate: $tappedCoordinate, shadowOverlay: shadowOverlay)
8385
.onChange(of: tappedCoordinate) { _ in
8486
showMapLongPressedSheet = tappedCoordinate != nil
8587
}
@@ -237,6 +239,9 @@ struct MapView: View {
237239
return
238240
}
239241
viewModel.fetchOSMDataFor(from: .visibleRect(mapView: mapView))
242+
let rect = mapView.visibleMapRect
243+
let extendedVisibleRect = rect.insetBy(dx: -rect.size.width * 0.25, dy: -rect.size.height * 0.25)
244+
shadowOverlay.addVisibleRect(extendedVisibleRect)
240245

241246
}
242247
}

GoInfoGame/GoInfoGame/UI/Map/ShadowOverlayRenderer.swift

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,29 @@ class ShadowOverlayRenderer: MKOverlayRenderer {
2020
context.setBlendMode(.clear)
2121
context.setAllowsAntialiasing(false)
2222
context.setShouldAntialias(false)
23-
24-
for mapRect in shadowOverlay.annotationRects {
25-
let cutout = self.rect(for: mapRect).integral
23+
24+
for rect in shadowOverlay.visibleRects {
25+
let cutout = self.rect(for: rect).integral
2626
context.fill(cutout)
2727
}
28-
28+
2929
context.setBlendMode(.normal)
3030
}
31+
32+
func addVisibleRect(_ rect: MKMapRect) {
33+
guard let shadowOverlay = overlay as? ShadowOverlay else { return }
34+
shadowOverlay.addVisibleRect(rect)
35+
setNeedsDisplay()
36+
}
3137
}
3238

3339
class ShadowOverlay: NSObject, MKOverlay {
3440
let coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
3541
let boundingMapRect: MKMapRect = MKMapRect.world
36-
let annotationRects: [MKMapRect]
37-
38-
init(annotations: [MKAnnotation]) {
39-
self.annotationRects = annotations.map { annotation in
40-
let center = MKMapPoint(annotation.coordinate)
41-
let width = MKMapPointsPerMeterAtLatitude(annotation.coordinate.latitude) * 500 // 500m box
42-
return MKMapRect(
43-
origin: MKMapPoint(x: center.x - width / 2, y: center.y - width / 2),
44-
size: MKMapSize(width: width, height: width)
45-
)
46-
}
47-
}
48-
}
49-
50-
42+
private(set) var visibleRects: [MKMapRect] = []
5143

52-
extension MKCoordinateRegion {
53-
func corners() -> [CLLocationCoordinate2D] {
54-
let latMin = center.latitude - span.latitudeDelta / 2
55-
let latMax = center.latitude + span.latitudeDelta / 2
56-
let lonMin = center.longitude - span.longitudeDelta / 2
57-
let lonMax = center.longitude + span.longitudeDelta / 2
58-
return [
59-
CLLocationCoordinate2D(latitude: latMin, longitude: lonMin),
60-
CLLocationCoordinate2D(latitude: latMin, longitude: lonMax),
61-
CLLocationCoordinate2D(latitude: latMax, longitude: lonMax),
62-
CLLocationCoordinate2D(latitude: latMax, longitude: lonMin)
63-
]
44+
func addVisibleRect(_ rect: MKMapRect) {
45+
visibleRects.append(rect)
6446
}
6547
}
6648

0 commit comments

Comments
 (0)