我正在使用iOS 11新API,并且我成功地使群集出现。现在,我尝试更改提供自定义图像的群集图像。自从我创建了这个自定义注释视图:
class PlaceView: MKAnnotationView {
override var annotation: MKAnnotation? {
willSet {
guard let place = newValue as? Place else {return}
clusteringIdentifier = Place.type
image = place.image
}
}
我试图在willSet块中添加这一行:
cluster?.image = UIImage(named: "Cluster")
但它没有用。
我错过了什么?有人能指出我正确的方向吗?
答案 0 :(得分:2)
您应该检查注释是否为MKClusterAnnotation
类型。如果是,则可以使用memberAnnotations
属性访问成员注释。例如,在您的情况下,您可以说:
override var annotation: MKAnnotation?
{
willSet
{
if let cluster = newValue as? MKClusterAnnotation {
image = UIImage(named: "Cluster")
} else {
// set image for non cluster
}
}
}
有关详细信息,请参阅WWDC 2017 What's New with MapKit.