注意:我使用的是iOS11s原生mapview注解集群。
在注释仍然以最大缩放方式聚集的情况下,我们可以用什么方式显示标注?
我展示了一个弹出式视图,用于显示集群中的批注列表,但是调用selectAnnotation还不足以显示“集群”批注的标注。
选择了"Something“,但没有显示标注。我的意思是,我的didDeselect view方法是在我接触mapview之后调用的。
发布于 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的弱引用,您必须选择集群方法:
//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)
}
}
}
}发布于 2019-11-12 07:28:58
这实际上很简单。如果分配的MKMarkerAnnotationView没有设置为通过.canShowCallout显示标注,并且视图上没有附件(这是很重要的),那么映射视图就不会显示标注。如果不满足这两个条件,那么地图可以在引脚本身上显示标题和副标题。所以,你需要做的就是:
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,则如果您没有设置标题和副标题,地图将不会显示标注。
https://stackoverflow.com/questions/47216252
复制相似问题