简体   繁体   English

地图注释showCallout-iOS

[英]Map Annotation showCallout-iOS

I have problem to show the annotation title as shown in the following images. 我无法显示注释标题,如下图所示。 First image denotes value very well; 第一张图片很好地表示了价值; on the other hand, once value goes up to three digits then title shows three dots as shown in the second image. 另一方面,一旦值上升到三位数,标题将显示三个点,如第二幅图像所示。 I would like to know how to fix this problem. 我想知道如何解决这个问题。 Any idea would be more than welcome!. 任何想法都将超过欢迎! Thanks a lot in advance, appreciated! 在此先多谢,感激不尽! I have just put my code here for reference! 我刚刚将代码放在这里供参考!

- (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;
  }

My showCallout title is updated in the following code: 我的showCallout标题已通过以下代码更新:

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

在此处输入图片说明在此处输入图片说明

The problem is that the callout view's layout is not recalculated when the title changes. 问题在于,标题更改时,不会重新计算标注视图的布局。 Maybe you should file a bug report for that. 也许您应该为此提交错误报告。

To force a relayout, you can deselect and select the annotation programmatically if the callout is visible. 要强制重排,可以在标注可见的情况下以编程方式取消选择并选择注释。 It's not animated, but it changes the width of the 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];
    }
}

You should also delete this line, as the title of the annotation is used as text label on the annotation anyway: 您还应该删除此行,因为注释的标题始终用作注释上的文本标签:

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

UPDATE: 更新:

The solution @detunized provided works with animation. @detunized提供的解决方案可与动画一起使用。 Instead of creating an unneeded UIView though, you can better remove and re-add the button view: 但是,与其创建不需要的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;
    }
}

I don't know the correct solution to this problem, but I have a hacky one for you. 我不知道该问题的正确解决方案,但我为您提供了一个不错的解决方案。 You need to trigger relayout of the view and it could be achieved by messing with the accessory view. 您需要触发视图的中继,可以通过弄乱附件视图来实现。 You're not using left accessory view here, so you can set it to something and the set it back to nil . 您此处未使用左附件视图,因此您可以将其设置为某物,然后将其设置回nil Like this: 像这样:

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

I tried and it works on iOS 5. Cannot test on iOS 6. 我尝试过,它可以在iOS 5上运行。无法在iOS 6上进行测试。

If you do that often then it's good not to create UIView all the time, but rather reuse something. 如果您经常这样做,那么最好不要一直创建UIView ,而要重用某些东西。

I had the same problem, when I was updating rightCalloutAccessoryView . 当我更新rightCalloutAccessoryView时,我遇到了同样的问题。 It only worked if I first set it to nil and then right away set it to whatever I needed in the first place. 仅当我首先将其设置为nil ,然后立即将其设置为我需要的任何内容时,它才起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM