地图注释showCallout-iOS

时间:2012-11-07 23:41:54

标签: ios ios6 mkmapview ios6-maps

我有问题要显示注释标题,如下图所示。第一张图片非常好地表示价值;另一方面,一旦值达到三位数,则标题显示三个点,如第二幅图像所示。我想知道如何解决这个问题。任何想法都会受到欢迎!非常感谢提前,谢谢!我刚把我的代码放在这里供参考!

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
   MKAnnotationView *pinView=nil;
    if(![annotation isKindOfClass:[Annotation class]]) // Don't mess user location
        return nil;

    static NSString *defaultPinID = @"StandardIdentifier";
    pinView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if (pinView == nil){
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
    }

    if ([annotation isKindOfClass:[Annotation class]]) {
        Annotation *a = (Annotation *)annotation;

        pinView.image = [ZSPinAnnotation pinAnnotationWithColor:a.color];
        pinView.annotation = a;
        pinView.enabled = YES;
        pinView.centerOffset=CGPointMake(6.5,-16);
        pinView.calloutOffset = CGPointMake(-11,0);

    }

    pinView.canShowCallout = YES;
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [pinView setRightCalloutAccessoryView:rightButton];
    pinView.leftCalloutAccessoryView = [[UIView alloc] init];
    pinView.leftCalloutAccessoryView=nil;

    return pinView;
  }

我的showCallout标题在以下代码中更新:

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

问题是标题更改时不会重新计算callout视图的布局。也许你应该为此提交一份错误报告。

要强制重新布局,如果标注可见,您可以以编程方式取消选择并选择注释。它不是动画,但它会改变标注的宽度:

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];


if ([self.mapView viewForAnnotation:annotation] != nil) {
    NSArray *selectedAnnotations = self.mapView.selectedAnnotations;
    if ((selectedAnnotations != nil) && ([self.mapView.selectedAnnotations indexOfObject:annotation] != NSNotFound)) {
        [self.mapView deselectAnnotation:annotation animated:NO];
        [self.mapView selectAnnotation:annotation animated:NO];
    }
}

您还应该删除此行,因为注释的标题仍然用作注释上的文本标签:

[rightButton setTitle:annotation.title forState:UIControlStateNormal];

<强>更新

@detunized提供的解决方案适用于动画。您可以更好地删除并重新添加按钮视图,而不是创建不需要的UIView:

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];

MKAnnotationView *annotationView = [self.mapView viewForAnnotation:annotation];
// The annotation must be visible, otherwise a refresh isn't needed
if (annotationView != nil) {
    NSArray *selectedAnnotations = self.mapView.selectedAnnotations;
    // The annotation must be selected, otherwise a refresh isn't needed
    if ((selectedAnnotations != nil) && ([self.mapView.selectedAnnotations indexOfObject:annotation] != NSNotFound)) {
            // Unset and re-set the right button to force relayout
            UIView *view = annotationView.rightCalloutAccessoryView;
            annotationView.rightCalloutAccessoryView = nil;
            annotationView.rightCalloutAccessoryView = view;
    }
}

答案 1 :(得分:0)

我不知道这个问题的正确解决方案,但我有一个hacky问题。您需要触发视图的重新布局,并且可以通过弄乱附件视图来实现。您没有在此处使用左侧附件视图,因此您可以将其设置为某个内容并将其设置回nil。像这样:

self.pin.title = @"Ozone Level: 100";
self.pin_view.leftCalloutAccessoryView = [[[UIView alloc] init] autorelease];
self.pin_view.leftCalloutAccessoryView = nil;

我试过,它适用于iOS 5.无法在iOS 6上测试。

如果你经常这样做,那么不要一直创建UIView,而是重用一些东西。

我在更新rightCalloutAccessoryView时遇到了同样的问题。只有当我第一次将其设置为nil然后立即将其设置为我首先需要的任何内容时,它才有效。