Is it possible to customize cluster image in iOS 11?

ε祈祈猫儿з 提交于 2020-03-24 01:01:10

问题


I'm using iOS 11 new APIs and I have been successful in making cluster appear. Now, I'm trying to change cluster image providing a custom image. Since I created this custom annotation view:

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

I tried to add this line inside willSet block:

cluster?.image = UIImage(named: "Cluster")

but it didn't work.

What am I missing? Can anyone point me in the right direction?


回答1:


You should check to see if the annotation is of type MKClusterAnnotation. If it is you can then use the memberAnnotations property to access the member annotations. In your case, for example, you could say:

override var annotation: MKAnnotation?
{
    willSet
    {
        if let cluster = newValue as? MKClusterAnnotation { 
            image = UIImage(named: "Cluster")
        } else {
            // set image for non cluster
        }
    }
}

For more information see WWDC 2017 What's New with MapKit.



来源:https://stackoverflow.com/questions/47100598/is-it-possible-to-customize-cluster-image-in-ios-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!