williamsandonz
williamsandonz

Reputation: 16440

Title isn't showing for Annotation in MKMapView

I've successfully gotten the title showing on a tap event for the users location, but I can't get it working for my own custom Annotation. I've tried setting title on it but it explicitly but that doesn't work, how can I achieve this?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *userMarkerView = nil; 
    PayMarkerAnnotationView *payMarkerView = nil;

    if(annotation != mapView.userLocation) 
    {
        payMarkerView = (PayMarkerAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"markerView"];
        if(payMarkerView == nil) {
            payMarkerView = [[PayMarkerAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"markerView"];
        }

        payMarkerView.annotation = annotation;
        return payMarkerView;
    } 
    else {
        [mapView.userLocation setTitle:@"You are here"];
        return userMarkerView;
    }
}

Class:

@interface PaymentMarker : NSObject <MKAnnotation> {
    NSString* type;
    NSString* address;
    float latitude;
    float longitude;
    NSString* title;
}


@property (nonatomic, copy) NSString* address;
@property (nonatomic, copy) NSString* type;
@property (nonatomic) float latitude;
@property (nonatomic) float longitude;
@property (nonatomic, copy) NSString* title;

//MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end



- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        NSString* imageName;

        if ([annotation isMemberOfClass:[PaymentMarker class]])
        {
            PaymentMarker *pm = (PaymentMarker *)self.annotation;
            if([pm.type isEqualToString:@"nzpost"]) {
                imageName = @"icon-map-post.png";
            } else {
                imageName = @"icon-map-incharge.png";
            }

            self.image = [UIImage imageNamed:imageName];
        }
    }
    return self;
}

Upvotes: 0

Views: 1075

Answers (1)

user467105
user467105

Reputation:

You need to set the annotation view's canShowCallout property to YES.

You can do this in the initWithAnnotation method inside the if block:

self.canShowCallout = YES;

self.backgroundColor = ...

Upvotes: 2

Related Questions