ios - MKAnnotation 文本到 UILabel

标签 ios uilabel mkannotation

您好,当我单击注释时,一个标签获取注释的标题而另一个标签获取副标题是否有可能是这样的:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
label1.text = annotation.title;
label2.text = annotation.subtitle;
}

最佳答案

由于 MKAnnotation 是一个协议(protocol),因此您必须定义自己的类来实现该协议(protocol)。

@interface MapAnnotation : NSObject<MKAnnotation> {

CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;    
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;


@end

然后将以上属性综合到.m文件中

@synthesize coordinate,title,subtitle;

在MapViewController类(即带有mkmapview的 Controller 类)中,您需要实现以下代码。

        MKCoordinateRegion region;
        region.center.latitude = latitude; // latitude float value for location
        region.center.longitude =longitude; // longitude float value for location
        region.span = MKCoordinateSpanMake(spanX, spanY);
        MapAnnotation *ann = [[MapAnnotation alloc] init];
        ann.title = @"Title";
        ann.subtitle = @"subTitle";
        ann.coordinate = region.center;
        [mapView addAnnotation:ann];

        [self.mapView setRegion:region animated:YES];
    [mapView setDelegate:self];

关于ios - MKAnnotation 文本到 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19388571/

相关文章:

ios - Swift 无法在 MKAnnotation 中分配字幕

ios - 去除 html 标签并在 UILabel Objective c 中显示

swift - 如何使 UILabel 在未跟踪图像时出现在屏幕上,并在跟踪图像时消失

ios - UILabel绘制/出现在屏幕上的相关方法

ios - 关于 MKAnnotation 和 NSString 内存泄漏问题的建议

swift - 如何获取 MKMapView 中隐藏了多少个注释?

ios - 为什么不同swift类中的函数没有被调用?

ios - Swift IOS 应用程序在哪里实例化 View 和 ViewController?

ios - NSDateformatter setDateFormat 方法崩溃

iOS 褪色的白色背景上的强白色文本?