iosdictionarymkmapviewmapkitmkannotation

Display MKMapViewAnnotations within a map view's visible rect


I'm displaying an MKMapView inside a Path-style parallax table view header. To create the effect, the mapView bounds is larger than the area visible to the user. I need to set the map view region such that all the map's annotations are contained within the visible rect of MKMapView. What's the best way to do this?

MKMapView with limited visible area

Edit for clarity: Here's a use-case. The mapView size is 320 x 380. The visible area, however, is defined by the rect (0.0, 20.0, 320.0, 100.0). I need to set the region such that all the annotations appear in this rect within the mapView.


Solution

  • Setting the map region so that all annotations are contained in a certain part of an MKMapView can be done in three steps. Input are the mapView and the annotationsFrame.

    1. Calculate an MKMapRect mapRect that contains all annotations.
    2. Calculate the padding insets from mapView.bounds and annotationsFrame.
    3. Call -setVisibleMapRect:edgePadding:animated: on the map view.

    Below is a screen shot of a test. The red overlay shows the annotationsFrame.

    screen Shot of a test showing that all annotations are inside the given annotation frame

    Here is the code. Beware: It's all in one method to simplify adding it to your code, and it is not tested for edge cases like passing in n annotations with the same coordinate, or having the annotations so far apart that the map would have to get zoomed out too much, or having coordinates that span the edge of the map at +/-180 degrees longitude.

    - (void)zoomAnnotationsOnMapView:(MKMapView *)mapView toFrame:(CGRect)annotationsFrame animated:(BOOL)animated
    {
        if (_mapView.annotations.count < 2) return;
    
    
        // Step 1: make an MKMapRect that contains all the annotations
    
        NSArray *annotations = _mapView.annotations;
    
        id <MKAnnotation> firstAnnotation = [annotations objectAtIndex:0];
        MKMapPoint minPoint = MKMapPointForCoordinate(firstAnnotation.coordinate);
        MKMapPoint maxPoint = minPoint;
    
        for (id <MKAnnotation> annotation in annotations) {
            MKMapPoint point = MKMapPointForCoordinate(annotation.coordinate);
            if (point.x < minPoint.x) minPoint.x = point.x;
            if (point.y < minPoint.y) minPoint.y = point.y;
            if (point.x > maxPoint.x) maxPoint.x = point.x;
            if (point.y > maxPoint.y) maxPoint.y = point.y;
        }
    
        MKMapRect mapRect = MKMapRectMake(minPoint.x, minPoint.y, maxPoint.x - minPoint.x, maxPoint.y - minPoint.y);
    
    
        // Step 2: Calculate the edge padding
    
        UIEdgeInsets edgePadding = UIEdgeInsetsMake(
            CGRectGetMinY(annotationsFrame),
            CGRectGetMinX(annotationsFrame),
            CGRectGetMaxY(mapBounds) - CGRectGetMaxY(annotationsFrame),
            CGRectGetMaxX(mapBounds) - CGRectGetMaxX(annotationsFrame)
        );
    
    
        // Step 3: Set the map rect
    
        [mapView setVisibleMapRect:mapRect edgePadding:edgePadding animated:animated];
    }
    

    If you go for a perfect placement (and who doesn't), here are three things to consider:

    1. The code assures that all the coordinates are in the annotationsFrame, but the annotations themselves may be outside. To prevent that, simply use more padding. For example, if your annotations are 20x20 and centered on the coordinate, use 10 more padding on all sides.
    2. Below iOS 7, the map was not zooming to the perfect zoom scale, but to the next tile size (power of two). So there will be more space around the annotations than needed, just as shown on the screenshot.
    3. On iOS 7, the map view will not only zoom perfectly, but automatically care about the status bar. To make the calculation correct, you need to subtract the status bar height from the top padding on iOS 7.