检查 MKMarkerAnnotationView 是否在 MKMapView 中混乱或整理
check if MKMarkerAnnotationView is cluttered or decluttered in MKMapView
参考苹果提供的以下示例,我完成了Clustering for Points。 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({ [=10=] is MKClusterAnnotation }) as! [MKClusterAnnotation]
for cluster in clusters {
if cluster.memberAnnotations.first(where: { [=10=] === annotation }) != nil {
return true
}
}
return false
}
用法: 从 mapView 中随机选取一个注释
let annotations = mapView.annotations.filter { [=11=] 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
MKMarkerAnnotationView
是 MKAnnotationView
的子类,您可以覆盖 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!)")
}
现在运行并点击地图上的注释并检查打印消息的调试区域。
参考苹果提供的以下示例,我完成了Clustering for Points。 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({ [=10=] is MKClusterAnnotation }) as! [MKClusterAnnotation]
for cluster in clusters {
if cluster.memberAnnotations.first(where: { [=10=] === annotation }) != nil {
return true
}
}
return false
}
用法: 从 mapView 中随机选取一个注释
let annotations = mapView.annotations.filter { [=11=] 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 inMKMapView
MKMarkerAnnotationView
是 MKAnnotationView
的子类,您可以覆盖 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!)")
}
现在运行并点击地图上的注释并检查打印消息的调试区域。