choefele/CCHMapClusterController

Subclassing CCHMapClusterAnnotation

Closed this issue · 5 comments

Hi, in my original cluster-free setup, I created a subclass of MKPointAnnotation, so I could add a reference to an object that contained additional information. (This is because my custom callout requires more info than title and subtitle). After adding CCHMapClusterController I changed my code to create a subclass of CCHMapClusterAnnotation to which I then added my additionally required properties. I can add these BZAnnotation : CCHMapClusterAnnotation instances to the mapClusterController - however, in mapView:viewForAnnotation: they always come back as CCHMapClusterAnnotation and the additional info is lost (even when the CCHMapClusterAnnotation is not a cluster) . How can I make a CCHMapClusterAnnotation keep a reference to my custom object? Thanks.

No need to subclass CCHMapClusterAnnotation – simply keep your custom annotation a subclass of MKPointAnnotation and add it to CCHMapClusterController. CCHMapClusterAnnotations are the container for clustered annotations. Your custom annotation will be inside the array stored in the annotations property.

This works great - vielen Dank!

HTKT commented

@briomusic : Can you give a sample code? I also have the same problem. THanks

@HTKT

Code example:

//This method is inside `MyCustomClusterAnnotationView` class
//and will return a custom pin callout only if contains a unique annotation
 private func setupCallout() -> UIView? {

    guard let calloutView = CustomCalloutView.instanciateFromNib() else {
      return nil
    }

    guard let clusterAnnotation = self.annotation as? CCHMapClusterAnnotation else {
      return nil
    }

    if !clusterAnnotation.isUniqueLocation() {
      return nil
    }
    //Like @choefele said, your custom annotation will be inside `annotations` property.
    guard let customAnnotation = clusterAnnotation.annotations.first! as? MyCustomAnnotation else {
      return nil
    }

    calloutView.lblTitle.text = customAnnotation.street
    calloutView.lblSubtitle.text = customAnnotation.city
    return calloutView
  }

Hi, I tried subclassing CCHMapClusterAnnotation cause I need to add an image property. So every cluster has a custom image. However, even if
let annotation = DiscoverMapAnnotation(title: "10% Weekend spree", locationName: "Nu Sentral", discipline: "Shopping Mall", coordinate: CLLocationCoordinate2D(latitude: 3.133369, longitude: 101.686874))
self.mapClusterController.addAnnotations([annotation], withCompletionHandler: nil)

let annotation = DiscoverMapAnnotation(title: "10% Weekend spree", locationName: "Nu Sentral", discipline: "Shopping Mall", coordinate: CLLocationCoordinate2D(latitude: 3.133369, longitude: 101.686874)) self.mapClusterController.addAnnotations([annotation], withCompletionHandler: nil)
my if let annotation = annotation as? DiscoverMapAnnotation still fails and will only react to if let annotation = annotation as? CCHMapClusterAnnotation {

Am I doing this wrongly?
Cause in my case every annotation should have a different image (passed from server)