当我在MKMapView启用MKUserTrackingModeFollowWithHeading时,我的MKAnnotationView存在问题。
我使用MKAnnotationView的centerOffset属性来定位我的图像。指定针尖相对于图像中心的坐标有些不直观,但我想出了以下公式:
annotationView.centerOffset = CGPointMake(imageWidth/2.0 - tipXCoordinate, imageHeight/2.0 - tipYCordinate);
这对于缩放地图的大小没有问题,标记针的顶部在地图上保持相对位置。
然而,当我启用
MKUserTrackingModeFollowWithHeading
时,它就不能正常工作了。标记针围绕图像中心旋转,而不是围绕其顶部。因此,当地图旋转时,标记针的顶部并未指向它们应该注释的位置。我已经尝试过一些
MKAnnotationView
的frame
和center
属性,但感觉它们对针的对齐没有任何影响。有趣的是,
MKPinAnnotationView
似乎根本不使用centerOffset
,而是使用了一个偏移的frame
。然而,我无法复制这个效果。改变自定义视图的frame
根本没有移动它。谢谢您提供的任何见解 :-)
解决方案:
不要使用centerOffset!使用
annotationView.layer.anchorPoint
代替。锚点的坐标系也更好。坐标范围从图像矩形的0.0(左上角)到1.0(右下角):annotationView.layer.anchorPoint = CGPointMake(tipXCoordinate/imageWidth, tipYCordinate/imageHeight);