2013-10-15 65 views
0

喜是有可能,當我點擊註釋,一個標籤獲取的註釋和其他標籤字幕的標題有些東西一樣:MKAnnotation文字的UILabel

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

回答

0

試試這個

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

註解可以通過view.annotation獲得(視圖是MKAnnotationView而不是id )。所以它應該是'(MapAnnotation *)view.annotation'。 – Anna

+0

謝謝安娜我編輯了我的答案 – Rocker

+0

與MKPointAnnotation * mAnnotation =(MKPointAnnotation *)view.annotation完美合作; – dastjuso

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]; 
+0

我使用JSON已經添加註解,現在我需要的東西是當註釋點擊標籤得到的標題和副標題的文本。 – dastjuso

+0

試一試此 - (無效)的MapView:(的MKMapView *)的MapView annotationView:(MKAnnotationView *)視圖calloutAccessoryControlTapped:(UIControl *)控制 { MapAnnotation * mAnnotation =(MapAnnotation *)視圖; label1.text = mAnnotation.title; label2.text = mAnnotation.subtitle; } – Rocker