问题出在您的保卫声明中。正如我在简短的研究中可以发现的那样,iOS使用MKClusterAnnotation类型来放置相同位置的注释。因此,该view
变量没有.annotation
可确认您的自定义子类的集合。
在这种情况下,您需要尝试将其强制转换为MKClusterAnnotation
对象,而该对象具有您想要.memberAnnotations
的type属性[MKAnnotation]
。
我认为您的代码应该看起来像这样(虽然尚未测试):
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let anno = view.annotation as? MyAnno {
print("Single Annotation selected:")
print(anno.post)
// do anything
return
}
if let anno = view.annotation as? MKClusterAnnotations {
let selection: [MKAnnotation] = anno.memberAnnotations
print("\(selection.count) Annotations selected:")
// do something with your annotation group
// you should now also be able to cast to your MyAnno class again
for item in selection {
if let myAnno = item as? MyAnno {
print(myAnno.post)
}
}
}
}
有关更多信息,请参见使用MapKit注释聚类对地图进行整理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句