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

标签 ios swift xcode mapkit ios11

我正在使用 iOS 11 的新 API,我已经成功地让集群出现了。现在,我正在尝试更改提供自定义图像的集群图像。由于我创建了这个自定义注释 View :

    class PlaceView: MKAnnotationView {
        override var annotation: MKAnnotation? {
            willSet {
                guard let place = newValue as? Place else {return}
                clusteringIdentifier = Place.type
                image = place.image
            }
    }

我试图在 willSet block 中添加这一行:

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.

关于ios - 是否可以在 iOS 11 中自定义集群图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47100598/

相关文章:

swift - Swift 4 中是否可以从父类修改子类的属性?

ios - Xcode 5 - iOS - 如何在没有 Storyboard 或 XIB 的情况下在 Xcode 中创建 View Controller 以及如何将其配置为 AppDelegate 作为 Root View

ios - 使文本更靠近 UILabel 的中间开始?

ios - 如何防止我的 UISplitViewController 在旋转到纵向时显示主 VC?

swift - UILabel 的 NSTextAlignment.Justified 不起作用

ios - 继承问题 - super 后预期为 '.' 或 '['

ios - 链接器命令失败,退出代码为 1(使用 -v 查看调用)xcode firebase

ios - 在pubspec.yaml中添加Google Maps软件包后,Flutter的iOS应用将无法构建

iphone - NSURLConnection 忽略 Keep-Alive 超时?

swift - 单个 UITableview 单元格中的多种字体大小(Swift)