selectAnnotation不显示标注

时间:2011-06-25 16:53:35

标签: objective-c cocoa-touch ios4 mkmapview

我想更改MKMapView上显示的标注。当我打电话

[self.mapView selectAnnotation:annotation animated:YES]; 

它将地图平移到注释,但不显示其标注。如何让它显示标注?

2 个答案:

答案 0 :(得分:2)

在注释对象上设置title和可选的subtitle属性。 MapKit会自动显示标注。

@interface MyAnnotation : NSObject <MKAnnotation> {
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

实施,

#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize title, subtitle;

@end

用法,

MyAnnotation *foo = [[MyAnnotation alloc] init];
foo.title = @"I am Foo";
foo.subtitle = "I am jus' a subtitle";

答案 1 :(得分:0)

您应该实现MKAnnotation Delegate方法

-(MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *AnnotationViewIdentifier = @"AnnotationViewIdentifier";

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationViewIdentifier];
    if (annotationView == nil) 
    {
       annotationView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationViewIdentifier]autorelease];
    }

    annotationView.canShowCallout = YES;

    return annotationView;
}