I have to fit the pins to the visible map so I get the MKMapRect with the well-known lines of code:
MKMapRect mapRect = MKMapRectNull;
for(id<MKAnnotation> annotation in [self.mapView annotations]){
if (![annotation isKindOfClass:[MKUserLocation class]]) {
MKMapPoint annotationPoint = MKMapPointForCoordinate([annotation coordinate]);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, delta, delta);
mapRect = MKMapRectUnion(mapRect, pointRect);
}
}
then I get and set the MKCoordinateRegion
with:
MKCoordinateRegion region = MKCoordinateRegionForMapRect(newMapRect);
MKCoordinateRegion fitRegion = [self.mapView regionThatFits:region];
[self.mapView setRegion:fitRegion animated:animated];
Since I use have two states (open and close map) close is where the map is not totally visible (the origin.y value on self.view
is negative) I need to redraw the rect when the method "fitToAnnotations" is called I use [self.mapView setNeedsLayout];
so I can get the correct self.mapView.frame
(open or close).
Now, If i use this code, with some others lines, in iOS7 everything works good but with iOS 7.1 the open-map-Region is different showing a big edge Region without pins and all fitted in the center portion.
Here is how i get the correct region to show if the map is full or half screen size
MKMapRect newMapRect = mapRect;
if (mapFullScreen) {
newMapRect = [self.mapView mapRectThatFits:mapRect edgePadding:UIEdgeInsetsMake(200, 15, 30, 15)];
}else{
MKMapView *smallMap = [[MKMapView alloc] initWithFrame:self.homeViewController.tableView.tableHeaderView.frame];
newMapRect = [smallMap mapRectThatFits:mapRect edgePadding:UIEdgeInsetsMake(15, 60, 5, 60)];
smallMap = nil;
}
what's going wrong?
I am facing the same issue with iOS 7.1.
In my app the problem is happening randomly, it's probably dependent on the execution speed of some internal map threads.
A dirty hack for me was to add an [NSThread sleepForTimeInterval:1.0] in the background thread just before the call of the method to zoom in the region (which is similar to your code described)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//(generating annotions array here)
//ios 7.1 hack:
[NSThread sleepForTimeInterval:1.0];
dispatch_async(dispatch_get_main_queue(), ^{
[self zoomMapViewToFitAnnotations:self.mapView annotations:[annotationsToAddArray allObjects] animated:YES];
//(addding the generated annotations here)
});
});