如何在MapView中为不同的注释点添加不同的标题和副标题?

时间:2012-10-08 14:45:43

标签: iphone ios5 maps mkannotation mkannotationview

我在mapview中显示不同的注释点。我想为所有不同的注释显示不同的标题和副标题。我以下面的方式实现了 viewForAnnotation 委托方法。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
{
    NSLog(@"annotation Class %@", [annotation class]);

if ([annotation isKindOfClass:[HGMovingAnnotation class]]) {

    static NSString *kMovingAnnotationViewId = @"HGMovingAnnotationView";

    HGMovingAnnotationView *annotationView = (HGMovingAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:kMovingAnnotationViewId];

    if (!annotationView) {
        annotationView = [[HGMovingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kMovingAnnotationViewId];
    }

    //configure the annotation view
    annotationView.image = [UIImage imageNamed:@"symbol-moving-annotation.png"];

    annotationView.bounds = CGRectMake(0, 0, 20, 20); //initial bounds (default)
    annotationView.mapView = mapView;

    NSLog(@"Inside Moving Annotation");
    return annotationView;
}

static NSString *kCustomAnnotationView = @"CustomAnnotationView";

MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:kCustomAnnotationView];

if ([annotation isKindOfClass:[CLLocation class]]) {
    MKAnnotationView *customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                          reuseIdentifier:kCustomAnnotationView];
    customAnnotationView.image = [UIImage imageNamed:@"customAnnotation"];

    customAnnotationView.canShowCallout = NO;
    customAnnotationView.centerOffset = CGPointMake(2,-12);
    NSLog(@"Inside Custom Annotation1");
    return customAnnotationView;
} else {
    customPinView.annotation = annotation;
    customPinView.pinColor = MKPinAnnotationColorPurple;
    NSLog(@"Inside Custom Annotation2");
    return customPinView;
}

}

我想显示 customPinView 注释的标题和子标题。请帮帮我。

2 个答案:

答案 0 :(得分:2)

注释视图没有标题或副标题。您需要子类MKAnnotation并在自定义注释上设置(子)标题。 viewForAnnotation方法不是正确的地方,您应该在[annotation setTitle:@"my title"]之前调用[mapView addAnnotation:annotation]

示例代码:

@interface MyCustomAnnotation : NSObject <MKAnnotation> {

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

@end

答案 1 :(得分:0)

我已经尝试了phix23建议的方法,并附带以下代码。它有效..

titleArray = [[NSArray alloc]initWithObjects:@"First Point",@"Second Point",@"Third Point",@"Fourth Point",@"Fifth Point", nil];
subTitleArray = [[NSArray alloc]initWithObjects:@"Restaurant",@"Park",@"Hotel",@"Theatre",@"AmusementPark", nil];

NSInteger customAnnotationCount = annotations.count;

//CLLocationCoordinate2D coordinates[customAnnotationCount];

for (NSInteger index = 0; index < customAnnotationCount; index++) {

    CLLocation *location = [annotations objectAtIndex:index];
    NSString *titleString = [titleArray objectAtIndex:index];
    NSString *subTitleString = [subTitleArray objectAtIndex:index];
    CLLocationCoordinate2D coordinate = location.coordinate;
    //coordinates[index] = coordinate;
    CustomAnnotations *customAnnotation = [[CustomAnnotations alloc]initWithCoordinates:coordinate placeName:titleString description:subTitleString];
    [_mapView addAnnotation:customAnnotation];
    [customAnnotation release];
}

“annotations”是包含位置点的数组。 _mapView是我的MKMapView。 CustomAnnotations是一个NSObject类,它以与顶部描述的相同方式实现MKAnnotation协议。