0

我在地图视图中显示不同的注释点。我想为所有不同的注释显示不同的标题和副标题。我以以下方式实现了我的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注释的标题和子标题。请帮我。

4

2 回答 2

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
于 2012-10-08T17:43:02.920 回答
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];
}

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

于 2012-10-09T12:12:05.117 回答