Nil与返回类型' MKClusterAnnotation'不兼容。

时间:2017-10-08 10:14:02

标签: ios swift mapkit ios11

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

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

但请看错误:Nil is incompatible with return type '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
        }
    }
}

1 个答案:

答案 0 :(得分:0)

该功能在MKMapViewDelegate中是可选的,因此您无需实现它。当你实现它时,它的返回值是非可选的。所以你必须返回MKClusterAnnotation,这就是你得到编译错误的原因。

尝试将displayPriority的{​​{1}}设置为MKAnnotationView

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