Display MKMapViewAnnotations within a map view's visible rect

后端 未结 7 633
梦毁少年i
梦毁少年i 2021-02-02 02:31

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

7条回答
  •  囚心锁ツ
    2021-02-02 03:08

    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  firstAnnotation = [annotations objectAtIndex:0];
        MKMapPoint minPoint = MKMapPointForCoordinate(firstAnnotation.coordinate);
        MKMapPoint maxPoint = minPoint;
    
        for (id  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.

提交回复
热议问题