Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detail callout accessory view #46

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

pauljohanneskraft
Copy link
Owner

based on the work of @RomanPodymov

@pauljohanneskraft
Copy link
Owner Author

I'm honestly thinking whether this is the best approach for this - maybe something like a view modifier on that annotation would be more suitable, but we would also deviate from the existing API there - but then again, afaik detailCalloutAccessory is not supported by the SwiftUI wrapper from MapKit anyways

@RomanPodymov
Copy link

Hello @pauljohanneskraft
Or you can keep a branch for "non-SwiftUI-API-features".

@pauljohanneskraft
Copy link
Owner Author

@RomanPodymov What do you think about a solution like this:

#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

It could then be used on any MapAnnotation using the following construct:

ViewMapAnnotation(...) {
    AnnotationContent()
}
.detailCalloutAccessory {
    DetailCalloutAccessoryContent()
}

@RomanPodymov
Copy link

Hello @pauljohanneskraft
Looks great. I don't have that much experience with SwiftUI, so I trust you.

@darronschall
Copy link

For what it's worth, it doesn't look like iOS 17 adds callouts to the SwiftUI Map component based on the initial beta documentation at https://developer.apple.com/documentation/mapkit/mapkit_for_swiftui

The proposed solution at #46 (comment) looks reasonable to me.

@darronschall darronschall mentioned this pull request Jun 8, 2023
@RomanPodymov
Copy link

Hello @darronschall
If it is similar to iOS 17 SwiftUI Map API we can try this way.

darronschall added a commit to darronschall/Map that referenced this pull request Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants