【问题标题】:MKAnnotionView does not show calloutMKAnnotionView 不显示标注
【发布时间】:2013-08-07 12:40:47
【问题描述】:

我已经实现了 MKAnnotionView:

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSString *identifier = @"mypin";

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if(annotationView == nil)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.canShowCallout = YES;
        UIImageView *pin = [[UIImageView alloc] initWithImage:[UIImage imageNamed:identifier]];
        UIImageView *shadow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_shadow"]];
        shadow.frame = CGRectMake(21, 36, 60, 27);

        [annotationView addSubview:shadow];
        [annotationView addSubview:pin];

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 11, 20);
        [btn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];

        annotationView.rightCalloutAccessoryView = btn;
        annotationView.centerOffset = CGPointMake(-(44.0f/2), -62.0f);
    }

    return annotationView;
}

图钉和阴影在地图上显示得很好,但是当我按下图钉时,现在会显示标注。我很肯定注释对象有一个标题和一个副标题值。

如果我使用 MKAnnotionView 上的 .image 属性添加我的图钉,它可以工作,但我的影子在图钉顶部.. 嗯! :/

出了什么问题?

【问题讨论】:

    标签: ios mkmapview mkmapviewdelegate


    【解决方案1】:

    由于您将图像添加为 annotationView 的子视图而不使用图像设置器,我相信您的 annotationView 的框架是CGRectZero。 (您可以通过将 clipToBounds 激活为 true 来轻松检查) 因此,您的 pin 没有 hitZone 来接收事件并显示标注。 您可能希望将 annotationView 的边界设置为两个图像的总大小(pin + shadow)。

    annotationView.bounds = CGRectMake(0, 0, pin.frame.size.width, pin.frame.size.width);
    //+Shadow ? if not : annotationView.bounds = pin.bounds;
    

    在不相关的注释中,我相信您在重用注释时可能会遇到问题,因为您没有将注释重置为 annotationView。 您可能希望将其更改为:

    if(annotationView ==nil) { }
    annotationView.annotation = annotation;
    return annotationView;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多