MKMapView ignores update of centerOffset in iOS 4

旧街凉风 提交于 2019-11-30 17:55:53

Make sure you are using MKAnnotationView and not MKPinAnnotationView! You can't set the centerOffset of a MKPinAnnotationView-object (except if you subclass of course).

I have the same problem - centerOffset seems to be taken into account only the first time. It is changed internally, but the view is not moved - so what you need to do is move the view yourself.

You can move the view by adjusting its center with the required offset - the selected view remains aligned at the top-left corner with the unselected view, so you need to realign their centers. Here's my case:

Selected -> Unselected:

self.center = CGPointMake(self.center.x + 56.0, self.center.y + 130.0);
self.centerOffset = CGPointMake(5.0, -14.0);

Unselected -> Selected:

self.center = CGPointMake(self.center.x - 56.0, self.center.y - 130.0);
self.centerOffset = CGPointMake(64.0, -81.0);

Where 130 is the difference in height between the views(center point is at the bottom), and 56 is the difference between the X offsets of their centers.

Remember - you still need to change the center offset because it'll be taken into account when zooming.

Hope this helps, I've lost a few hours on this. Remember to submit a bug report to Apple.

Vijay Shankar

I think instead of centerOffset you can use setRegion which works fine in all versions.

CGPoint point = [mapView convertCoordinate:selectedAnnotation.coordinate toPointToView:self.view];

CGRect frame = [customView frame];
frame.origin.y = point.y - frame.size.height;
frame.origin.x = point.x - frame.size.width / 2;

MKCoordinateRegion region = [mapView convertRect:frame toRegionFromView:self.view];
[mapView setRegion:region animated:YES];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!