在 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
,这就是您收到编译错误的原因。
尝试将所有 MKAnnotationView
的 displayPriority
设置为 MKFeatureDisplayPriority.required
。
参见:https://developer.apple.com/documentation/mapkit/mkfeaturedisplaypriority/2867301-required
关于ios - Nil 与返回类型 'MKClusterAnnotation' 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46629940/