objective-c - MKMapView 中的注释未显示标题

标签 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;
}

最佳答案

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

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

self.canShowCallout = YES;

self.backgroundColor = ...

关于objective-c - MKMapView 中的注释未显示标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12083879/

相关文章:

ios - Swift:有 2 页的 ScrollView

ios - 将 Obj-c iOS 应用程序转换为 Swift 时出现链接器错误

ios - 如何在堆栈中存储 NSString

ios - Xcode 6 中的 Parse 和 Facebook SDK 导入冲突 ("ParseFacebookUtils"无法识别)

ios - 需要轻量级迁移吗?

ios - 创建请求循环

objective-c - AVPlayer 实例可以同时播放两个视频吗?

ios - 创建可滑动的应用程序介绍屏幕

ios - 如何使用蓝点在我的 MKMapView 上显示我的当前位置和区域?

ios - 自定义标注气泡 iOS