У меня есть карта с большим количеством аннотаций, которые я хочу кластеризовать, используя собственную реализацию кластера. Кроме того, отдельные выводы подкласса MKAnnotationView
для создания пользовательского представления.
Когда я запускаю приложение и уменьшаю масштаб, поведение соответствует ожидаемому, группа выводов. Когда я нажимаю на MKAnnotation
, я создаю новый вид, показывающий больше информации, и после этого контакты не группируются снова. Я думаю, что viewFor Annotation
метод является виновником этого.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// let the OS show user locations itself
if annotation is MKUserLocation {
return nil
}
// What do I do here?
else if annotation is MKClusterAnnotation {
return nil
}
// Create my custom annotation view
let annotationView = CustomAnnotationView(...)
// ...
return annotationView
}
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
return MKClusterAnnotation(memberAnnotations: memberAnnotations)
}
Чтобы активировать кластеризацию на карте, я добавил clusteringIdentifier
в моем подклассе MKAnnotationView
следующим образом:
class CustomAnnotationView: MKAnnotationView {
private let annotationFrame = CGRect(x: 0, y: 0, width: 40, height: 40)
private var label: UILabel = UILabel()
private var bg: UIView = UIView()
private let selectedLabel = UILabel(frame: CGRect(x: 0, y: -50, width: 100, height: 40))
public var number: UInt32 = 0 {
didSet {
self.label.text = String(number)
}
}
public var station: String = "" {
didSet {
self.selectedLabel.text = station
}
}
override func prepareForDisplay() {
super.prepareForDisplay()
self.displayPriority = .defaultHigh
}
func updateBackground(color: UIColor) {
self.bg.backgroundColor! = color
}
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
clusteringIdentifier = "\(CustomAnnotationView.self)"
// Do the View customization work
}
func setTitle(forStation name: String) {
// ...
}
// ...
}
Я не знаю, пропустил ли я что-то, но могу без проблем взаимодействовать с картой, но после некоторых взаимодействий аннотации не группируются.