0

嗨,当我单击注释时,是否有可能一个标签获取注释的标题,而另一个标签获取副标题,例如:

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

2 回答 2

1

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

@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 的控制器类)中,您需要实现以下代码。

        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];
于 2013-10-15T19:32:26.600 回答
0

尝试这个

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view   calloutAccessoryControlTapped:(UIControl *)control
 {
   MapAnnotation *mAnnotation = (MapAnnotation*)view.annotation; 
   label1.text = mAnnotation.title;
   label2.text = mAnnotation.subtitle;
  }
于 2013-10-15T21:13:51.507 回答