forked from pauljohanneskraft/Map
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This was authored by @pauljohanneskraft in the comment at pauljohanneskraft#46 (comment)
- Loading branch information
1 parent
2d8951c
commit 6162e36
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// DetailCalloutAccessory.swift | ||
// Map | ||
// | ||
// Created by Darron Schall on 10/4/23. | ||
// | ||
|
||
#if !os(watchOS) | ||
|
||
import Foundation | ||
import MapKit | ||
import SwiftUI | ||
|
||
private struct DetailCalloutAccessory<Annotation: MapAnnotation, Content: View>: MapAnnotation { | ||
|
||
// MARK: Static Functions | ||
|
||
static func registerView(on mapView: MKMapView) { | ||
Annotation.registerView(on: mapView) | ||
} | ||
|
||
// MARK: Stored Properties | ||
|
||
private let wrappedAnnotation: Annotation | ||
private let content: () -> Content | ||
|
||
// MARK: Computed Properties | ||
|
||
var annotation: MKAnnotation { | ||
wrappedAnnotation.annotation | ||
} | ||
|
||
// MARK: Initialization | ||
|
||
init(_ annotation: Annotation, @ViewBuilder content: @escaping () -> Content) { | ||
self.wrappedAnnotation = annotation | ||
self.content = content | ||
} | ||
|
||
// MARK: Methods | ||
|
||
func view(for mapView: MKMapView) -> MKAnnotationView? { | ||
guard let view = wrappedAnnotation.view(for: mapView) else { | ||
return nil | ||
} | ||
|
||
if Content.self != EmptyView.self { | ||
view.canShowCallout = true | ||
view.detailCalloutAccessoryView = NativeHostingController(rootView: content()).view | ||
} else { | ||
view.canShowCallout = false | ||
view.detailCalloutAccessoryView = nil | ||
} | ||
|
||
return view | ||
} | ||
|
||
} | ||
|
||
extension MapAnnotation { | ||
|
||
public func detailCalloutAccessory<Content: View>(@ViewBuilder content: @escaping () -> Content) -> some MapAnnotation { | ||
DetailCalloutAccessory(self, content: content) | ||
} | ||
|
||
} | ||
|
||
#endif |