首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以显示MKClusterAnnotation标注?

是否可以显示MKClusterAnnotation标注?
EN

Stack Overflow用户
提问于 2017-11-10 13:34:21
回答 2查看 838关注 0票数 0

注意:我使用的是iOS11s原生mapview注解集群。

在注释仍然以最大缩放方式聚集的情况下,我们可以用什么方式显示标注?

我展示了一个弹出式视图,用于显示集群中的批注列表,但是调用selectAnnotation还不足以显示“集群”批注的标注。

选择了"Something“,但没有显示标注。我的意思是,我的didDeselect view方法是在我接触mapview之后调用的。

EN

回答 2

Stack Overflow用户

发布于 2017-11-27 19:12:06

我也遇到过同样的问题。在这种情况下,他们似乎没有仔细考虑。您必须选择MKClusterAnnotation,而不是集群的MKAnnotation,但要实现这一点并不简单。

在iOS11上,MKAnnotationView上有一个名为cluster的属性,正如文档所述,它是:

If non-nil this is the annotation view this view is clustered into.

因此,在我的MKAnnotationView子类中,我覆盖了setSelected方法,并且使用对mapView的弱引用,您必须选择集群方法:

代码语言:javascript
复制
//You have weak reference to mapView
weak var mapView: MKMapView?

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    if #available(iOS 11.0, *) {
        if selected, let cluster = cluster {
            // Deselect the current annotation (Maybe this step is not required, didn't check it)
            if let annotation = annotation {
                mapView?.deselectAnnotation(annotation, animated: false)
            }
            // Select the cluster annotation
            if let clusterAnnotation = cluster.annotation {
                mapView?.selectAnnotation(clusterAnnotation, animated: true)
            }
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-11-12 07:28:58

这实际上很简单。如果分配的MKMarkerAnnotationView没有设置为通过.canShowCallout显示标注,并且视图上没有附件(这是很重要的),那么映射视图就不会显示标注。如果不满足这两个条件,那么地图可以在引脚本身上显示标题和副标题。所以,你需要做的就是:

代码语言:javascript
复制
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{   

    if annotation is MyAnnotationConformingClass {
       let annotation = annotation as! MKAnnotation

       let view = MKAannotationView(annotation: annotation, reuseIdentifier: "pinReUserId")
       view.canShowCallout = true
       view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
       return view
    }

    if annotation is MKClusterAnnotation {
        let annotation = annotation as! MKClusterAnnotation
        let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "ClusterResuseId")
        view.canShowCallout = true
        view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        return view
    }

    return nil
}

通过为集群的MKMarkerAnnotationView提供一个附件并允许显示标注,该标注将被显示出来。如果您还记得较早版本的SDK,则如果您没有设置标题和副标题,地图将不会显示标注。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47216252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档