I've managed to draw numbers inside custom colored circle annotations (based on this). I want to make some optimizations for my custom annotation class and I read about reusing. My problem is if I make the stuff reusable, the annotation views get mixed on map which is a big problem. The custom drawn annotation views cannot be reused ? Or is it somehow related to the view's annotaion ? I mean, the annotation stores the number to be drawn on its view, practically it's a 1to1 relationship between annotation and its view.
Here is my relevant code : Custom annotationview's init :
-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageType:(int)imageType {
self = [super initWithAnnotation: annotation reuseIdentifier: reuseIdentifier];
if (self != nil)
{
if ([annotation isKindOfClass:[CircleMarker class]])
{
// custom annotation class with some extra fields
CircleMarker * clm = (CircleMarker * )annotation;
self.locationMarker = clm;
// ... setting frame and other stuff
self.image = [self getImage]; /* this method DRAWS image based on clm */
self.canShowCallout = NO;
}
...
}
And the delegate :
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *reuseId_small = @"smallcircle";
static NSString *reuseId_big = @"bigcircle";
CircleAnnotationView * nca = nil;
if ((int)[self.mapView getZoomLevel] < ZOOM_LEVEL_FOR_NUMBERS)
{
nca = (CircleAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId_small];
if (nca == nil )
nca = [[[CircleAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId_small imageType:2] autorelease];
}
else
{
nca = (CircleAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId_big];
if ( nca == nil )
nca = [[[CircleAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId_big imageType:1] autorelease];
}
return nca;
}
I've tried to replace the self.image =
part with a custom drawRect
function, but the result was the same.
Thanks.