问题
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