hulab / ClusterKit

An iOS map clustering framework targeting MapKit, Google Maps and Mapbox.
MIT License
512 stars 86 forks source link

Clusters sometimes become invisible on map zoom in / zoom out #41

Closed staticdreams closed 5 years ago

staticdreams commented 6 years ago

This seem to be happening only when a cluster "has been used" to reveal underlined annotations and becomes hidden when it's time to put them back in.

I have followed the Swift example and my code is largely based on in except for the part where I apply dynamic text on the clusters (cluster count)

impishkaleidoscopicgermanspaniel-size_restricted

Here's my func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if (annotation is MKUserLocation) {
        return nil
    }

    guard let cluster = annotation as? CKCluster else {
        return nil
    }

    if cluster.count > 1 {
        let cView = mapView.dequeueReusableAnnotationView(withIdentifier: ClusterAnnotationIdentifier) ??
            CKClusterView(annotation: annotation, reuseIdentifier: ClusterAnnotationIdentifier, count: cluster.count)
        return cView
    }

    return mapView.dequeueReusableAnnotationView(withIdentifier: PointAnnotationIdentifier) ??
        CKAnnotationView(annotation: annotation, reuseIdentifier: PointAnnotationIdentifier)

}

And annotations code:

class CKAnnotationView: MKAnnotationView {

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        canShowCallout = true
        isDraggable = false
        image = UIImage(named: "blue-marker")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("Not implemented")
    }
}

class CKClusterView: MKAnnotationView {

    convenience init(annotation: MKAnnotation?, reuseIdentifier: String?, count: UInt) {
        self.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        let icon = "\(count)".to(image: UIImage(named: "cluster-marker")!, atPoint: CGPoint(x: 0, y: 8))
        image = icon
    }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        image = nil
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("Not implemented")
    }
}

class ParkingPointAnnotation: NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }
}

Any suggestion where I can take a look in order to debug this?

franck-nadeau commented 6 years ago

Hello,

Have you been able to resolve this, because I am seeing the same thing on my side?

staticdreams commented 6 years ago

@franck-nadeau I switched to Mapbox instead and it seemed to work fine