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 に答える