【问题标题】:How to add different titles and subtitles to different Annotation points in a MapView?如何为 MapView 中的不同 Annotation 点添加不同的标题和副标题?
【发布时间】:2012-10-08 14:45:42
【问题描述】:

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

【问题讨论】:

    标签: iphone ios5 maps mkannotation mkannotationview


    【解决方案1】:

    注释视图没有标题或副标题。您需要继承MKAnnotation 并在自定义注释上设置(子)标题。 viewForAnnotation 方法不适合这个,你应该在[mapView addAnnotation:annotation] 之前调用[annotation setTitle:@"my title"]

    示例代码:

    @interface MyCustomAnnotation : NSObject <MKAnnotation> {
    
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    @property (nonatomic) CLLocationCoordinate2D coordinate;
    
    @end
    

    【讨论】:

      【解决方案2】:

      我已经尝试了 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 类,方式与上面描述的相同。

      【讨论】:

        猜你喜欢
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        • 2020-04-28
        • 2013-06-13
        • 2018-11-28
        • 2019-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多