添加注释后,自定义标注全部显示在地图上

时间:2014-11-05 03:25:35

标签: ios mkmapview mkannotation mkannotationview mkpinannotationview

我正在创建自定义MKPinAnnotationView,并在将注释添加到地图视图后。所有这些都在地图上显示。但是我不想要那个,我只想在点击引脚然后显示标注时打开标注。现在我的代码如下:

CustomAnnotationView.h

@interface CustomAnnotationView : MKPinAnnotationView

@property (nonatomic, strong) AnnotationView *annotationView;

@end

CustomAnnotationView.m

@implementation CustomAnnotationView

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        self.annotationView = (AnnotationView *)[[[NSBundle mainBundle] loadNibNamed:@"AnnotationView" owner:self options:nil] lastObject];
        [self addSubview:self.annotationView];
    }
    return self;
}

MapViewController.m

- (void)viewDidLoad
{
    __weak typeof(self) weakSelf = self;
    [[Client sharedClient] searchWithParamaters:parameters completionBlock:^(NSArray *businesses, NSError *error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (!error) {
            [strongSelf.mapView addAnnotations:businesses];
        }
    }];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *identifer = @"Cell";
    CustomAnnotationView *pin = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifer];
    if (pin == nil) {
        pin = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifer];
    }

    pin.canShowCallout = YES;
    pin.animatesDrop = YES;

    Place *place = annotation;

    pin.annotationView.businessTitle.text = place.name;
    pin.annotationView.businessDetailLabel.text = place.address;
    [pin.annotationView.ratingImageView sd_setImageWithURL:[NSURL URLWithString:place.ratingImageSmallUrl]];

    return pin; 
    }
    return nil;
}

0 个答案:

没有答案