How to set the rotation center of an MKAnnotationView
Asked Answered
I

1

9

I've got a problem with my MKAnnotationViews when MKUserTrackingModeFollowWithHeading is enabled on the MKMapView.

I positioned my images using the centerOffset property of the MKAnnotationView. Specifying the coordinates of the pin's tip relative to the coordinate system at the center of the image is somewhat counter-intutive, but I came up with the following formula:

annotationView.centerOffset = CGPointMake(imageWidth/2.0 - tipXCoordinate, imageHeight/2.0 - tipYCordinate);

This works fine for zooming the map in and out. The tips of the pins keep their relative position on the map.

However, when I enable MKUserTrackingModeFollowWithHeading, it won't work anymore. The Pins rotate around the center of the image, instead of the tip. So when the map rotates, the tips do no point to the locations they are supposed to annotate.

I've played around a bit with the frameand centerproperties of the MKAnnotationView, but I feel, they are having no effect on the alignement of the pins whatsoever.

Interestingly, the MKPinAnnotationView does not seem to use centerOffset at all, but a shifted frame instead. However, I was unable to reproduce this. Changing the frame of my custom view did not move it at all.

Thanks for any insights you can provide :-)

Solution:

Don't use centerOffset! Use annotationView.layer.anchorPoint instead. The coordinate system of achor point is much nicer, too. Coordinates range from 0.0 (top/left) to 1.0 (bottom/right) of the image rectangle:

annotationView.layer.anchorPoint = CGPointMake(tipXCoordinate/imageWidth, tipYCordinate/imageHeight);
Iapetus answered 24/7, 2012 at 11:32 Comment(0)
S
13

A friend asks me to let you know that you should "try this for instance":

self.layer.anchorPoint = CGPointMake (0.5f, 1.0f);
Sincerity answered 24/7, 2012 at 15:59 Comment(1)
Tell your friend, (s)he's my personal hero of the week :-) Many thanks!Iapetus

© 2022 - 2024 — McMap. All rights reserved.