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

Add anchorPoint to ViewMapAnnotation #53

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

Conversation

darronschall
Copy link

NOTE: This builds on #48, since I also needed those attributes to help control how my markers cluster. If #48 gets merged first, this PR will be slightly easier to review.

I originally tried just adding anchorPoint to ViewMapAnnotation and passing it through to the MKMapAnnotationView (using UIView's anchorPoint if available, otherwise falling back on centerOffset), but the anchor point wasn't applying correctly.

I borrowed from @allenhumphreys PR #40 to get the frame and the anchor point to apply correctly. I wasn't sure if that PR was ever going to get merged; I took the minimum changes necessary to get anchor point working in hopes of getting this PR merged.

I also looked at ideas in #38, which seems somewhat related. This could potentially fix #14 as well; I've noticed that my pins have a much more consistent location now during zoom/pan and seem to be anchored properly in the correct location.

My first pass made anchorPoint a CGPoint, but I decided it would be better to use UnitPoint as that's more SwiftUI-y. I can change it back to CGPoint if you prefer.

FWIW, I'm using this branch successfully in an iOS app, and I have pin-style markers. Setting anchorPoint: .bottom (the bottom middle of the marker) keeps the pins in the correct location as the user moves the map around in my app.

This uses `anchorPoint` from `UIView` when available (iOS 16+), otherwise it relies on `MKAnnotationView` `centerOffset` and converts the `anchorPoint` percentages into pixel values.
After applying the frame fix in the previous commit, the annotations displayed on the map with a white background. This brings back the clear background they previously had.
`UnitPoint` - https://developer.apple.com/documentation/swiftui/unitpoint - is more SwiftUI-y than `CGPoint`, and will allow the anchorPoint to use the constants like `.center` and `.bottom`.

Extract extensions to make it trivial to convert the `UnitPoint` to a `centerOffset` within a rect, and to convert back to a `CGPoint` for the `UIView` `anchorPoint` property.
The `#available(iOS 16)` wasn't preventing the `anchorPoint` from being set, but that is only available on `UIView`, not `NSView`. Ensuring `UIKit` is available fixes the error, and we fall back to `centerOffset` otherwise.
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.

MapAnnotations not centered properly
1 participant