MKMapView中的标注未显示标题

时间:2012-08-23 02:17:06

标签: objective-c ios mkmapview

我已经成功地在用户位置的点击事件中显示标题,但我无法让它适用于我自己的自定义注释。我已尝试在其上设置标题,但它明确但但是不起作用,我该如何实现?

- (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;
    }
}

类别:

@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;
}

1 个答案:

答案 0 :(得分:2)

您需要将注释视图的canShowCallout属性设置为YES

您可以在initWithAnnotation块内的if方法中执行此操作:

self.canShowCallout = YES;

self.backgroundColor = ...