简体   繁体   中英

CCHMapClusterController with Core Data NSManagedObject Subclass

I have an app in which I am using an MKMapView to display 200 pins in. The data from the pins is coming from Core Data and I have an Objective-C Category on the Core Data object's NSManagedObject subclass extending them to MKAnnotation so I can directly add the array of fetched objects from Core Data into the Map View. The problem is, because the pins were so close to each other, I started using CCHMapClusterController and the pin objects are no longer the category, they get converted to a CCHMapClusterAnnotation object. I was previously in my viewForAnnotation just grabbing the NSManagedObject subclass (Location) from the pin, but I can no longer do this as the pin objects are not Location objects anymore, they are CCHMapClusterObjects. Here is my old viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:     (id<MKAnnotation>)annotation {
static NSString *reuseID = @"EAnnotation";
MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseID];
if ([annotation isKindOfClass:[Location class]]) {

if (!view) {
    view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseID];
    view.canShowCallout = YES;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 46, 46)];

    Location *location = (Location *)annotation;

    [imageView setImage:location.smallpic];

    view.leftCalloutAccessoryView = imageView;

    view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

     }

  }

   return view;
}

My question is, how can I obtain which Location object is being tapped on when you are zoomed in all the way in the map? I understand when you are zoomed out the pins are not the object type Location but when I are zoomed all the way in, I should be able to access which Location object I am tapping.

CCHMapClusterAnnotation has a property called annotations , which contains an array of your annotation objects in this cluster (the Core Data objects in your case).

See this example if you want to show a different pin icon depending on the number of annotations in a cluster. The idea is that you use one custom annotation view for the clusters and inside the annotation view, you decide what icon to display.

Note: I'm the author of CCHMapClusterController – feel free to contact me if you have more questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM