Display MKMapViewAnnotations within a map view's visible rect

后端 未结 7 755
梦毁少年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:13

    if you want to find the annotations that are in a given rect:

    - (NSArray*)allAnnotationsInMapRect:(MKMapRect)mapRect {
        NSMutableArray *annotationsInRect = [NSMutableArray array];
        for(id

    and to assure the annotation VIEWS are in the rect, get the region for the annotations, then walk through them and get each view's bounds see if the bounds fit inside the visibleRect of the map and if not modify the region!

    ~~ like this:

    - (void)assureAnnotationViewsAreVisible:(NSArray*)annotations originalRegion:(MKCoordinateRegion)originalRegion {
        CGFloat smallestX = MAXFLOAT;
        CGFloat smallestY = MAXFLOAT;
        CGFloat biggestX = -100;
        CGFloat biggestY = -100;
    
        //NSLog(@"---: %d", annotations.count);
        for(id *annotation in annotations) {
            UIView *annotationView = [self.mapView viewForAnnotation:v];
    
            CGRect annotationViewFrame = annotationView.bounds;
            annotationViewFrame.origin = [self.mapView convertCoordinate:annotationView.coordinate toPointToView:self.mapView];
            annotationViewFrame.origin = CGPointMake(annotationViewFrame.origin.x-annotationViewFrame.size.width/2,
                                                     annotationViewFrame.origin.y-annotationViewFrame.size.height);
    
            smallestX = MIN(annotationViewFrame.origin.x, smallestX);
            smallestY = MIN(annotationViewFrame.origin.y, smallestY);
            biggestX = MAX(annotationViewFrame.origin.x+annotationViewFrame.size.width, biggestX);
            biggestY = MAX(annotationViewFrame.origin.y+annotationViewFrame.size.height, biggestY);
        }
        //NSLog(@"---");
    
        CGRect bounds = self.mapView.bounds;
        if(smallestX < bounds.origin.x || smallestY < bounds.origin.y || biggestX > bounds.origin.x+bounds.size.width || biggestY > bounds.origin.y+bounds.size.height) {
            CGRect neededRect = bounds;
            neededRect.origin = CGPointMake(MIN(bounds.origin.x, smallestX), MIN(bounds.origin.y, smallestY));
            neededRect.size = CGSizeMake(MAX(bounds.size.width, biggestX), MAX(bounds.size.height, biggestY));
    
            MKCoordinateRegion neededRegion = [self.mapView convertRect:neededRect toRegionFromView:self.mapView];
            _ignoreRegionChange = YES;
            [self.mapView setRegion:originalRegion animated:NO];
            _ignoreRegionChange = NO;
            [self.mapView setRegion:neededRegion animated:YES];
        }
        else {
            MKCoordinateRegion currentRegion = self.mapView.region;
            _ignoreRegionChange = YES;
            [self.mapView setRegion:originalRegion animated:NO];
            _ignoreRegionChange = NO;
            [self.mapView setRegion:currentRegion animated:YES];
        }
    }
    

提交回复
热议问题