MKMapRect zooms too much

后端 未结 5 1783
悲哀的现实
悲哀的现实 2021-01-14 11:32

I use this code to show all my annotations on my map:

 MKMapRect zoomRect = MKMapRectNull;
        for (id  annotation in mapView.annotat         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-14 12:06

    Ariel's answer didn't work for me, but I made a few small changes to it and it's working great now (especially with maps with a single pin):

    double minimumZoom = 6000; // for my purposes the width/height have same min zoom
    BOOL needChange = NO;
    
    double x = MKMapRectGetMinX(zoomRect);
    double y = MKMapRectGetMinY(zoomRect);
    double w = MKMapRectGetWidth(zoomRect);
    double h = MKMapRectGetHeight(zoomRect);
    double centerX = MKMapRectGetMidX(zoomRect);
    double centerY = MKMapRectGetMidY(zoomRect);
    
    if (h < minimumZoom) {  // no need to call MKMapRectGetHeight again; we just got its value!
        // get the multiplicative factor used to scale old height to new,
        // then apply it to the old width to get a proportionate new width
        double factor = minimumZoom / h;
        h = minimumZoom;
        w *= factor;
        x = centerX - w/2;
        y = centerY - h/2;
        needChange = YES;
    }
    
    if (w < minimumZoom) {
        // since we've already adjusted the width, there's a chance this
        // won't even need to execute
        double factor = minimumZoom / w;
        w = minimumZoom;
        h *= factor;
        x = centerX - w/2;
        y = centerY - h/2;
        needChange = YES;
    }
    
    if (needChange) {
        zoomRect = MKMapRectMake(x, y, w, h);
    }
    
    [mapView setVisibleMapRect:zoomRect animated:YES];
    

提交回复
热议问题