Question

In my app I use this code to zoom in my map where my marker are positioned :

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

    count++;

    if (count == arrayResults.count){

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

}

this work fine in iOS 6 but not in iOS 7, do you know why? thanks

Was it helpful?

Solution

This code relies on very specific delegate calling sequences.

The count++ implies it assumes the didAddAnnotationViews delegate method will be called immediately and separately after each annotation is added.

This is an unsafe assumption regardless of iOS version.

Since this zooming code doesn't rely on the annotation views to do the zooming (it just needs the annotation models -- ie. the id<MKAnnotation> objects), it doesn't need to be in didAddAnnotationViews in the first place.

Just put the zooming code (the part inside the if block), right after the code that adds the annotations (presumably after a for loop that goes through arrayResults). That should work in any iOS version.


Also note that in iOS 7, the new showAnnotations:animated: method makes this manual map rect construction unnecessary. In iOS 7, you can just do:

[mapView showAnnotations:mapView.annotations animated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top