gpt4 book ai didi

swift - 检查 MKMarkerAnnotationView 是否在 MKMapView 中困惑或整理

转载 作者:行者123 更新时间:2023-11-28 05:47:00 24 4
gpt4 key购买 nike

我已经通过引用苹果提供的以下示例对点进行了聚类。 https://developer.apple.com/documentation/mapkit/mkannotationview/decluttering_a_map_with_mapkit_annotation_clustering

我想知道是否有任何方法在注释困惑和整理时被调用。

或者

当我需要为此运行一些代码块时,我如何才能知道特定注释是杂乱无章的。

最佳答案

How I can come to know that particular annotation is cluttered and decluttered.

检查 MKClusterAnnotation.memberAnnotations 是否存在 MKAnnotation,如下所示:

func isCluttered(annotation: MKAnnotation) -> Bool {
let clusters = mapView.annotations.filter({ $0 is MKClusterAnnotation }) as! [MKClusterAnnotation]
for cluster in clusters {
if cluster.memberAnnotations.first(where: { $0 === annotation }) != nil {
return true
}
}
return false
}

用法:从 mapView 中随机选取一个注释

let annotations = mapView.annotations.filter { $0 is Cycle }
let randomIndex = Int(arc4random_uniform(UInt32(annotations.count)))
if (isCluttered(annotation: annotations[randomIndex])) {
print("Cluttered")
} else {
print("Not cluttered")
mapView.selectAnnotation(annotations[randomIndex], animated: true)
}

check if MKMarkerAnnotationView is cluttered or decluttered in MKMapView

MKMarkerAnnotationViewMKAnnotationView 的子类,你可以覆盖setSelected(_:animated:),例如打开 ClusterAnnotationView.swift 从您链接的示例代码中粘贴:

override func setSelected(_ selected: Bool, animated: Bool) {
let cluster = annotation as? MKClusterAnnotation
print("\(selected ? "Selecting" : "Deselected") Clustered Annotation \(cluster?.memberAnnotations.count ?? -1)")
}

同样,您可以覆盖 CycleAnnotationView.swift 中每个 MKMarkerAnnotationView 中的 setSelected(_:animated:) 方法,粘贴所有 3 个类:

override func setSelected(_ selected: Bool, animated: Bool) {
print("\(selected ? "Selecting" : "Deselected") unclustered annotation with type: \(clusteringIdentifier!)")
}

现在运行并点击 map 上的注释并检查打印消息的调试区域。

关于swift - 检查 MKMarkerAnnotationView 是否在 MKMapView 中困惑或整理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54287640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com