首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MKMapKit。显示所有MKAnnotation的中心映射

MKMapKit。显示所有MKAnnotation的中心映射
EN

Stack Overflow用户
提问于 2012-05-24 16:05:45
回答 1查看 462关注 0票数 1

我在地图上有2-10个注解别针。当我点击一个按钮时,我希望地图缩小并居中,这样用户就可以看到所有的图钉。我该怎么做呢?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-24 16:18:55

假设您有一个包含所有注释的数组,您可以执行以下操作:

代码语言:javascript
运行
复制
CLLocationCoordinate2D leftTop = CLLocationCoordinate2DMake(-90,180);
CLLocationCoordinate2D rightBottom = CLLocationCoordinate2DMake(90, -180);

for (int i=0; i < [annotations count]; i++) {
    id<MKAnnotation> annotation = (id<MKAnnotation>)[annotation objectAtIndex:i];
    CLLocationCoordinate2D coord = annotation.coordinate;
    if (coord.latitude > leftTop.latitude) {
        leftTop.latitude = coord.latitude;
    }
    if (coord.longitude < leftTop.longitude) {
        leftTop.longitude = coord.longitude;
    }
    if (coord.latitude < rightBottom.latitude) {
        rightBottom.latitude = coord.latitude;
    }
    if (coord.longitude > rightBottom.longitude) {
        rightBottom.longitude = coord.longitude;
    }
}

MKCoordinateSpan regSpan = MKCoordinateSpanMake(leftTop.latitude-rightBottom.latitude, rightBottom.longitude-leftTop.longitude);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(leftTop.latitude-regSpan.latitudeDelta/2, leftTop.longitude+regSpan.longitudeDelta/2);
regSpan.latitudeDelta = MAX(regSpan.latitudeDelta, 0.01);
regSpan.longitudeDelta = MAX(regSpan.longitudeDelta, 0.01);
MKCoordinateRegion reg = MKCoordinateRegionMake(center, regSpan);
if (CLLocationCoordinate2DIsValid(center)) {
    [_mapView setRegion:reg animated:YES];
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10733496

复制
相关文章

相似问题

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