hulab / ClusterKit

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

my custom annotation can not cast down CKCluster #18

Closed mitty810609 closed 6 years ago

mitty810609 commented 6 years ago

This is my custom annotation

class CafeShopAnnotation: MKPointAnnotation {
    override var coordinate: CLLocationCoordinate2D {
        get {
            return CLLocationCoordinate2DMake(self.latitude, self.longitude)
        }
        set {
            self.coordinate = newValue
        }
    }
    override var title: String? {
        get {
            return self.name
        }
        set {
            self.title = newValue
        }
    }
    override var subtitle: String? {
        get {
            return self.address
        }
        set {
            self.subtitle = newValue
        }
    }

    var id = ""
    var city = ""
    var name = ""
    var wifi = Double()
    var seat = Double()
    var quiet = Double()
    var tasty = Double()
    var cheap = Double()
    var music = Double()
    var address = ""
    var latitude = Double()
    var longitude = Double()
    var url = ""
    var limited_time = ""
    var socket = ""
    var standing_desk = ""
    var mrt = ""
    var open_time = ""

}

In viewdidload i will handle my store class transform to custom annotation

but view for annotation cast ckcluster fail

what should i do ?

maxep commented 6 years ago

First your CafeShopAnnotation should adopt the CKAnnotation protocol. Then, you should add your annotations to the mapView.clusterManager.annotations (not to the mapView)

mitty810609 commented 6 years ago

Cool brother, but i have another question.

Mapview annotation view only have black view , why ?

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

        let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotation") ??
            MKAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
       if annotation.isKind(of: MKUserLocation.self) {
            return nil

        } else if let cluster = annotation as? CKCluster {

            if cluster.count > 1 {
                annotationView.canShowCallout = true
                annotationView.image = UIImage(named: "cluster")
            } else {
                annotationView.canShowCallout = true
                annotationView.image = UIImage(named: "marker")
            }

        }
        return annotationView
    }

And i see your website ClusterKit DEMO , have a message count on annotation view, how to do that ?

maxep commented 6 years ago

For the count in cluster, please refer to #11 All the view are black even if you zoom in?

NB: canShowCallout of a cluster view should be set to false if you wan to zoom in on touch.

mitty810609 commented 6 years ago

Yes , even zoom in, and after zoom in limited i check the cluster count return 2.

didSelect view.Annotation only coordinate have value, title and subtitle are nil.

maxep commented 6 years ago

title and subtitle are nil because it's a cluster annotation. Are you sure that your data set has no duplicates, or annotations with the same position ?