0

I have this code where in iOS6 I center a marker in the center in a mapview, it don't happen in iOS 7 why?

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {

    MKMapRect zoomRect = MKMapRectNull;
    for (id <MKAnnotation> annotation in mapView.annotations)
    {
        MKMapPoint annotationP = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationP.x, annotationP.y, 7000, 7000);
        if (MKMapRectIsNull(zoomRect)) {
            zoomRect = pointRect;
        } else {
            zoomRect = MKMapRectUnion(zoomRect, pointRect);
        }
    }

    [mapView setVisibleMapRect:zoomRect animated:YES];

}
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • not sure of the bug, but since you are starting from annotation.coordinate setRegion is simpler. Don't calculate MKMapPoints, just setRegion using the existing region span, and your desired annotation.coordinate as the center! – RobP Apr 14 '14 at 16:02
  • can you show me an example? – cyclingIsBetter Apr 14 '14 at 16:04
  • The didAddAnnotationViews delegate method gets called at slightly different times in iOS 7 as compared to iOS 6. This code might be assuming that it's called immediately after you add an annotation. What is your goal here exactly? To center on _one_ annotation or to show _all_ annotations (not just one)? –  Apr 14 '14 at 16:25
  • Moving the center of your map to a new point is as simple as `[mapView setRegion:MKCoordinateRegionMake(newCenterCoords, mapView.region.span)]` but if you also want to get the union of a lot of coordinates and change the span as well, it gets more complicated. @Anna asked a good question, is the goal to show all annotations as in the code? or center on one as in the title? – RobP Apr 14 '14 at 16:40
  • This existing code is fine if you want to show all annotations (assuming the delegate method gets called after they've all been added). Only strange thing is the rect width of 7000 MKMapPoints which you might be doing for padding but there are better ways to do that in MKMapView. Your main issue I think is that you are assuming this method gets called right after adding an annotation. If you just want to center on one annotation, you can do that like @RobP said right after adding the annotation (_you don't need this delegate method_). –  Apr 14 '14 at 17:35
  • ok @RobP your code '[mapView setRegion:MKCoordinateRegionMake(newCenterCoords, mapView.region.span)]' work for center my annotation (I have one annotation), but at the same time I want to zoom on it and your code don't do it – cyclingIsBetter Apr 15 '14 at 08:55
  • @nazz_areno, use MKCoordinateRegionMakeWithDistance and set the distance in meters that you want to zoom to. –  Apr 15 '14 at 11:51

0 Answers0