gpt4 book ai didi

ios - MKAnnotation 文本到 UILabel

转载 作者:行者123 更新时间:2023-11-29 03:34:17 25 4
gpt4 key购买 nike

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

- (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com