是否可以在 iOS 11 中自定义集群图像?

Is it possible to customize cluster image in iOS 11?

我正在使用 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")

但是没用。

我错过了什么?谁能指出我正确的方向?

您应该检查注释是否属于 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.