ios - Nil 与返回类型 'MKClusterAnnotation' 不兼容

标签 ios swift mapkit ios11

在 iOS 11 中,默认行为是 map View 中的注释在缩小时聚集在一起。我不希望这种情况发生在我的用例中,因此我尝试禁用此默认行为。我尝试通过从可选的 MKMapViewDelegate 方法返回 Nil 来禁用它,如下所示:

@available(iOS 11.0, *)
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
    return nil // error on this line
}

但看到错误:Nil 与返回类型“MKClusterAnnotation”不兼容

我也尝试过以下方法,但没有成功:

@available(iOS 11.0, *)
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
    return MKClusterAnnotation.init(memberAnnotations: [])
}

编辑:

根据 Marcel的建议我实现了以下内容,但我仍然看到相同的行为:

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    for view in views {
        if #available(iOS 11.0, *) {
            view.displayPriority = .required
        }
    }
}

最佳答案

该函数在MKMapViewDelegate中是可选的,因此您不必实现它。当您实现它时,它的返回值是非可选的。因此,您必须返回 MKClusterAnnotation,这就是您收到编译错误的原因。

尝试将所有 MKAnnotationViewdisplayPriority 设置为 MKFeatureDisplayPriority.required

参见:https://developer.apple.com/documentation/mapkit/mkfeaturedisplaypriority/2867301-required

关于ios - Nil 与返回类型 'MKClusterAnnotation' 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46629940/

相关文章:

ios - "EXC_BAD_ACCESS"与 "Segmentation fault"。两者实际上都相同吗?

ios - 如何设置一个整数值作为导航栏的标题?

ios - 你如何检查 SwiftUI 是否处于预览模式?

iphone - MapView 高于 iOS 应用程序中的所有对象

ios - 像 Google Maps Directions 一样绘制 MKMapView Overlay

ios - 在 dispatch_async 中上传 UIImage

iphone - 将单元格的重新排序属性设置为 NO

objective-c - 在Xcode 4.3.2和iPhone Simulator 5.1下找不到sqlite3库

ios - 在 Xcode Scenes Swift iOS 中集成 Facebook 和 Twitter

iOS:对象化 MKCoordinateSpan 以放入数组