MKMapRect缩放太多了

时间:2011-12-11 15:30:28

标签: iphone xcode

我使用此代码在我的地图上显示我的所有注释:

 MKMapRect zoomRect = MKMapRectNull;
        for (id <MKAnnotation> annotation in mapView.annotations)
        {
            MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
            MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 1000);
            if (MKMapRectIsNull(zoomRect)) {
                zoomRect = pointRect;
            } else {
                zoomRect = MKMapRectUnion(zoomRect, pointRect);
            }
        }
        [mapView setVisibleMapRect:zoomRect animated:YES];

但我的问题是,当注释彼此接近时,由于矩形很小,它会缩放太多。

有什么方法可以解决这个问题吗?

5 个答案:

答案 0 :(得分:7)

在我的代码中,我添加了额外的间距,因此它会自动调整缩放级别以适应。

[aMapView setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(-100, -50, -50, -50) animated:YES];

答案 1 :(得分:1)

Ariel的回答对我不起作用,但我对它做了一些小改动,现在效果很好(特别是对于带有单个引脚的地图):

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];

答案 2 :(得分:1)

Ariel的解决方案对我也不起作用,但是n00neimp0rtant的解决方案却对我不起作用。我已经在Swift中将其重写为MKMapRect扩展中的函数rectWithMinimumZoom(_ minimumZoom: Double)。作为回报,它是调整后的矩形(如果需要调整)。另外,我还添加了一个额外的安全性,即minimumZoom不能除以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
}

答案 3 :(得分:0)

试试这段代码:

//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];

已编辑 - &gt;

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];

答案 4 :(得分:0)

对于那些喜欢一只内胆的人:

let minRectSize: Double = 5000
zoomRect = MKMapRect(
    x: zoomRect.minX - max(0, minRectSize - zoomRect.width) / 2,
    y: zoomRect.minY - max(0, minRectSize - zoomRect.height) / 2,
    width: max(zoomRect.width, minRectSize),
    height: max(zoomRect.height, minRectSize))