ios - MKMapView 缩放以显示框架中的所有注释

标签 ios mkmapview mapkit

我有一张全屏 map ,我想缩放到 map 上所有注释都可见的级别,但仅在应用程序的内容区域中可见,如此图所示:

enter image description here

如您所见, map 填满了整个屏幕,但是在顶部和底部覆盖了覆盖 map 的矩形。内容区域是亮绿色的部分。给定一组注释,我希望能够缩放 map ,以便所有内容都在该绿色内容区域内可见,而不是在 map 的实际框架内。

我在这里使用这个(类别)方法,它执行默认的缩放任务以适应 map 的框架。但我对如何修改它以仅考虑内容区域感到困惑:

- (void)zoomToShowAnnotations:(NSArray *)annotations extraPaddingMultiplier:(CGFloat)multiplier
{
    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(id<MKAnnotation> annotation in annotations)
    {
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
    }

    MKCoordinateRegion region;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;

    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * (multiplier != 0 ? multiplier : 1); // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * (multiplier != 0 ? multiplier : 1); // Add a little extra space on the sides

    [self setRegion:region animated:YES];
} 

最佳答案

根据文档,此方法应该适用。

// Position the map such that the provided array of annotations are all visible to the fullest extent possible.
@available(iOS 7.0, *)
open func showAnnotations(_ annotations: [MKAnnotation], animated: Bool)

你必须设置一个注释数组并将其放入函数中,如下所示

    let annotation = MKPointAnnotation()
    annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11)
    mapView.addAnnotation(annotation)
    mapView.showAnnotations([annotation], animated: true)

关于ios - MKMapView 缩放以显示框架中的所有注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072751/

相关文章:

iphone - 在 iPhone 上作为 MapKit 叠加层进行高效 KML 渲染

ios - 如何在另一个类中获取 map 注释属性?

ios - 避免弹出窗口在水平紧凑的环境中适应全屏

ios - AVPlayer URL https 与 ATS 的连接被阻止的明文 HTTP

ios - FCM 通知不会出现在 iOS 12 中

ios - startMonitoringForRegion 与 CLRegion :containCoordinate

objective-c - MKPolygon 的渲染标题

ios - NSURLSession 是否支持 FTP 上传?

iphone - MapKit 未删除所有注释

swift - map View 上的倒圆圈叠加(MapKit、MKOverlay、Swift)