问题
I think I may have found a bug in MKMapView
, but I'd like to run it by folks to see if anyone else has run into this issue. I'm using iOS 7 and Xcode 5.
In my storyboard, I have a view controller with an MKMapView
in it. My app allows rotation to both portrait and landscape. I am using autolayout to keep the frame of the MKMapView
updated when the user rotates the device.
After rotation, it appears that MKMapView
is reporting an incorrect centerCoordinate
in the regionDidChangeAnimated
delegate method:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"%@ %@", @(mapView.centerCoordinate.latitude), @(mapView.centerCoordinate.longitude));
}
When my view controller first loads in portrait orientation, I get the following output:
25.69266565483518 -40
37.17818069458006 -96.05458068847656
After I rotate the device to landscape orientation, I get the following output:
9.656197790830817 -96.05458068847656
After I rotate the device back to portrait orientation, I get the following output:
37.17818069458006 -96.05458068847656
Finally, here are screenshots of both portrait and landscape orientations. I drew dots over the center of the images to show that--at least from a rendering perspective--the centers are the same.
Is this actually a bug as it appears or am I missing something?
EDIT:
I changed the code in regionDidChangeAnimated
so that the centerCoordinate
is outputted after a one millisecond delay. When I do this, I get correct results. However, this is not optimal and I still think that centerCoordinate
should be updated by MKMapView
before regionDidChangeAnimated
is called.
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^
{
NSLog(@"%@ %@", @(mapView.centerCoordinate.latitude), @(mapView.centerCoordinate.longitude));
});
}
37.17818069458006 -96.05458068847656
37.17818069458006 -96.05458068847656
37.17818069458006 -96.05458068847656
37.17818069458006 -96.05458068847656
EDIT:
I created a sample project in order to reproduce the bug for an Apple bug report. What I noticed that if the MKMapView is not embedded within a navigation controller, the centerCoordinate property always reports the correct value. As soon as I embedded it within a navigation controller--and turned off the "extend edges under top bars and bottom bars" options--it reported incorrect values.
回答1:
The defect I submitted to Apple was confirmed to be a duplicate. Looks like this is, indeed, a bug in MapKit.
来源:https://stackoverflow.com/questions/21615551/bug-with-mkmapviews-centercoordinate-during-rotation