MKMapRect zooms too much

后端 未结 5 1825
悲哀的现实
悲哀的现实 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:41

    Ariel's solution wasn't working for me either but n00neimp0rtant's was. I have rewritten it in Swift as a function rectWithMinimumZoom(_ minimumZoom: Double) in an extension of MKMapRect. In return it the adjusted rect (if adjustment is needed). Also I added a extra safety that minimumZoom cannot be divided by 0.

    SWIFT

    extension MKMapRect {
    func rectWithMinimumZoom(_ minimumZoom: Double = 750) -> MKMapRect{
        var needChange = false
    
        var x = MKMapRectGetMinX(self)
        var y = MKMapRectGetMinY(self)
        var w = MKMapRectGetWidth(self)
        var h = MKMapRectGetHeight(self)
        let centerX = MKMapRectGetMidX(self)
        let centerY = MKMapRectGetMidY(self)
    
        if(h < minimumZoom){
            let factor = minimumZoom / max(h,1)
            h = minimumZoom
            w *= factor;
            x = centerX - w / 2
            y = centerY - h / 2
            needChange = true
        }
        if(w < minimumZoom){
            let factor = minimumZoom / max(w,1)
            w = minimumZoom
            h *= factor
            x = centerX - w / 2
            y = centerY - h / 2
            needChange = true
        }
        if(needChange){
            return MKMapRectMake(x, y, w, h);
        }
        return self
    }
    

提交回复
热议问题