问题描述
我正在使用iOS 11的新API,并且已经成功使集群出现.现在,我尝试更改提供自定义图像的群集图像.由于我创建了此自定义注释视图,因此:
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
}
}
我试图将这一行添加到willSet块中:
I tried to add this line inside willSet block:
cluster?.image = UIImage(named: "Cluster")
但是没有用.
我想念什么?谁能指出我正确的方向?
What am I missing? Can anyone point me in the right direction?
推荐答案
您应检查注解的类型是否为MKClusterAnnotation
.如果是,则可以使用memberAnnotations
属性访问成员注释.例如,您可能会说:
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
}
}
}
有关更多信息,请参阅WWDC 2017 MapKit的新增功能.
For more information see WWDC 2017 What's New with MapKit.
这篇关于是否可以在iOS 11中自定义群集映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!