MKMapView ignores update of centerOffset in iOS 4
Asked Answered
M

3

12

I previously created a custom callout bubble as a subview to the MKAnnotationView because the built in callout is so limited. This requires me to change to centerOffset of the MKAnnotationView when it is selected to account for the size of the callout bubble. This all worked perfectly before iOS 4 came out. Now, with iOS 4, it completely ignores my updating of the centerOffset property and therefore the pin and bubble appear to jump down and to the right (the top left corner of the callout bubble is now at the location where the pin point should be).

Does anyone know why this has changed in iOS 4? Is there something I can do to get the MKMapView to recognize the new centerOffset? Is this a bug that apple introduced?

Thanks for the help!

Magnetoelectricity answered 28/6, 2010 at 22:7 Comment(1)
Just wanted to pitch in, encountered the same issue. I've currently adjusted the annotation center to compensate for the offset change. It's far from ideal though.Pumpkin
C
6

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).

Chill answered 22/1, 2014 at 15:45 Comment(0)
O
4

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.

Oysterman answered 29/3, 2011 at 12:24 Comment(0)
L
0

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];
Landwehr answered 28/2, 2011 at 12:55 Comment(3)
We're not trying to adjust the region of the map, but the center offset of the actual annotation view.Pumpkin
@Yannick, yes, but setting the region with the same dimensions but a new center coordinate WILL re-center the map. I'd say this is the answer, having had the same trouble that you have had.Papoose
It's not what I'm trying to achieve. The current map region has to stay the same. I want the annotation view to move according to its new centerOffset property.Pumpkin

© 2022 - 2024 — McMap. All rights reserved.