MKMapRect zooms too much

后端 未结 5 1813
悲哀的现实
悲哀的现实 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 11:57

    Try this code:

    //here comes your for loop...
    
    double minMapHeight = 10; //choose some value that fit your needs
    double minMapWidth = 10;  //the same as above
    BOOL needChange = NO;
    
    double x = MKMapRectGetMinX(zoomRect);
    double y = MKMapRectGetMinY(zoomRect);
    double w = MKMapRectGetWidth(zoomRect);
    double h = MKMapRectGetHeight(zoomRect);  //here was an error!!
    
    if(MKMapRectGetHeight(zoomRect) < minMapHeight){
        x -= minMapWidth/2;
        w += minMapWidth/2;
        needChange = YES;
    }
    if(MKMapRectGetWidth(zoomRect) < minMapWidth){
        y -= minMapHeight/2;
        h += minMapHeight/2;
        needChange = YES;
    }
    if(needChange){
        zoomRect = MKMapRectMake(x, y, w, h);
    }
    [mapView setVisibleMapRect:zoomRect animated:YES];
    

    EDITED ->

    double minMapHeight = 250; //choose some value that fit your needs
    double minMapWidth = 250;  //the same as above
    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(MKMapRectGetHeight(zoomRect) < minMapHeight){
        //x -= minMapWidth/2;
        //w += minMapWidth/2;
        x = centerX - w/2;
        needChange = YES;
    }
    if(MKMapRectGetWidth(zoomRect) < minMapWidth){
        //y -= minMapHeight/2;
        //h += minMapHeight/2;
        y = centerY - h/2;
        needChange = YES;
    }
    if(needChange){
        zoomRect = MKMapRectMake(x, y, w, h);
    }
    [mapView setVisibleMapRect:zoomRect animated:YES];
    

提交回复
热议问题