将 MKMapView 缩放到 MKAnnotationView 的框架

eri*_*ric 6 xcode objective-c mapkit ios

我有一个要放大的分组图钉。当然,我知道引脚的坐标,并且它的视图是 rect。我只想将地图缩放到正确的区域,以便群集完全展开显示所有引脚(加上一点填充)。这样做的好方法是什么?

旁注:
在我的设置中,当缩放级别增加时,集群引脚将自动扩展为单个引脚,所以我在那里很好。我需要知道的是如何根据集群引脚的框架和坐标将 MapView 设置为新区域。

在此处输入图片说明

tan*_*ita 5

您可以使用:

mapView.showAnnotations(groupedAnnotations, animated: true)
Run Code Online (Sandbox Code Playgroud)

此方法将自动放大并定位地图,以便注释全部可见。


小智 1

首先移除组别针

[mapView removeAnnotation:groupAnnotation];
Run Code Online (Sandbox Code Playgroud)

然后在簇中添加引脚

[mapView addAnnotations:clusterAnnotations];
Run Code Online (Sandbox Code Playgroud)

然后确定要缩放的区域

CLLocationDegrees minLat = 90;
CLLocationDegrees maxLat = -90;
CLLocationDegress minLong = 180;
CLLocationDegrees maxLong = -180
[clusterAnnotations enumerateUsingBlock:^(id<MKAnnotation> annotation, NSUInteger idx, BOOL *stop) {
    CLLocationCoordinate2D coordinate = annotation.coordinate;
    minLat = MIN(minLat, coordinate.latitude);
    maxLat = MAX(maxLat, coordinate.latitude);
    minLong = MIN(minLong, coordinate.longitude);
    maxLong = MAX(maxLong, coordinate.longitude);
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat)/2.f, (minLong + maxLong)/2.f);
MKCoordinateSpan span = MKCoordinateSpanMake((maxLat - minLat)*1.25, (maxLong - minLong)*1.25); //1.25 is for padding
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
Run Code Online (Sandbox Code Playgroud)