首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapRect缩放过多

MKMapRect缩放过多
EN

Stack Overflow用户
提问于 2011-12-11 23:30:28
回答 5查看 4.4K关注 0票数 3

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

代码语言:javascript
复制
 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];

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

有什么办法解决这个问题吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-12-12 00:24:07

尝试以下代码:

代码语言:javascript
复制
//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];

已编辑的->

代码语言:javascript
复制
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];
票数 0
EN

Stack Overflow用户

发布于 2013-08-29 00:45:28

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

代码语言:javascript
复制
[aMapView setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(-100, -50, -50, -50) animated:YES];
票数 7
EN

Stack Overflow用户

发布于 2014-01-28 02:44:10

Ariel的答案对我不起作用,但我对它做了一些小改动,现在它工作得很好(特别是使用单个图钉的地图):

代码语言:javascript
复制
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];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8465149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档