MKMapRect缩放太多了 [英] MKMapRect zooms too much

查看:85
本文介绍了MKMapRect缩放太多了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

But my problem is that when the annotations are close to each other, it zooms too much as the rectangle is small.

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

Any way to fix this?

推荐答案

试试这段代码:

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

这篇关于MKMapRect缩放太多了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆