您好,当我单击注释时,一个标签获取注释的标题而另一个标签获取副标题是否有可能是这样的:
- (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/