Skip to content

Commit

Permalink
Added Anchor Point to ViewMapAnnotation
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov-kaiumov committed Mar 4, 2023
1 parent 96339ca commit 46bbb10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/Annotations/MKMapAnnotationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MKMapAnnotationView<Content: View>: MKAnnotationView {
backgroundColor = .clear
#endif

let controller = NativeHostingController(rootView: mapAnnotation.content)
let controller = NativeHostingController(rootView: mapAnnotation.content, ignoreSafeArea: true)

#if canImport(UIKit)
controller.view.backgroundColor = .clear
Expand All @@ -54,6 +54,10 @@ class MKMapAnnotationView<Content: View>: MKAnnotationView {

self.controller = controller
self.invalidateIntrinsicContentSize()

if let anchor = mapAnnotation.anchor {
centerOffset = .init(x: anchor.x * intrinsicContentFrame.width / 2, y: anchor.y * intrinsicContentFrame.height / 2)
}
}

// MARK: Overrides
Expand Down
5 changes: 5 additions & 0 deletions Sources/Annotations/ViewMapAnnotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,33 @@ public struct ViewMapAnnotation<Content: View>: MapAnnotation {

public let annotation: MKAnnotation
let clusteringIdentifier: String?
let anchor: CGPoint?
let content: Content

// MARK: Initialization

public init(
coordinate: CLLocationCoordinate2D,
anchor: CGPoint = .init(x: 0, y: 0),
title: String? = nil,
subtitle: String? = nil,
clusteringIdentifier: String? = nil,
@ViewBuilder content: () -> Content
) {
self.annotation = Annotation(coordinate: coordinate, title: title, subtitle: subtitle)
self.anchor = anchor
self.clusteringIdentifier = clusteringIdentifier
self.content = content()
}

public init(
annotation: MKAnnotation,
anchor: CGPoint = .init(x: 0, y: 0),
clusteringIdentifier: String? = nil,
@ViewBuilder content: () -> Content
) {
self.annotation = annotation
self.anchor = anchor
self.clusteringIdentifier = clusteringIdentifier
self.content = content()
}
Expand Down

0 comments on commit 46bbb10

Please sign in to comment.