在MKMapView上启用MKAnnotationView时,我的MKAnnotationView出现问题。
我使用MKAnnotationView的centerOffset属性定位了我的图像。 指定针尖相对于图像中心的坐标系的坐标有点反直觉,但是我提出了以下公式:
annotationView.centerOffset = CGPointMake(imageWidth/2.0 - tipXCoordinate, imageHeight/2.0 - tipYCordinate);这适用于放大和缩小地图。 引脚的尖端保持其在地图上的相对位置。
但是,当启用MKUserTrackingModeFollowWithHeading ,它将不再工作。 引脚围绕图像的中心旋转,而不是尖端。 所以当地图旋转时,提示并不指向他们应该注释的位置。
我已经玩了一下MKAnnotationView的frame和center属性,但我觉得,它们对引脚的MKAnnotationView没有任何影响。
有趣的是, MKPinAnnotationView似乎根本没有使用centerOffset ,而是使用了移位frame 。 但是,我无法重现这一点。 改变我的自定义视图的frame根本没有移动它。
感谢您提供的任何见解:-)
解:
不要使用centerOffset! 改为使用annotationView.layer.anchorPoint 。 achor点的坐标系也更好。 坐标范围从图像矩形的0.0(上/左)到1.0(下/右):
annotationView.layer.anchorPoint = CGPointMake(tipXCoordinate/imageWidth, tipYCordinate/imageHeight);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);最满意答案
一位朋友请我告诉你,你应该“尝试这个例子”:
self.layer.anchorPoint = CGPointMake (0.5f, 1.0f);A friend asks me to let you know that you should "try this for instance":
self.layer.anchorPoint = CGPointMake (0.5f, 1.0f);更多推荐
发布评论