【问题标题】:Force MKMapView viewForAnnotation to update强制 MKMapView viewForAnnotation 更新
【发布时间】:2012-06-02 04:36:58
【问题描述】:

所以我有一个添加了所有引脚的 MKMapView,引脚的颜色取决于是否为该引脚设置了值。当我第一次加载应用程序时,会调用 viewForAnnotation 并相应地设置颜色。但是,当我更新 pin 的详细信息(例如位置、标题等)时,我也会更新 pinColour 以发现它没有更新。看起来viewForAnnotation 在初始添加后没有再次调用。

我已经阅读了很多类似的问题,我可以确认mapView.delegate = self;

这是我的viewForAnnotation 代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MapAnnotation *)annotation
{
    if([annotation class] == MKUserLocation.class)
        return nil;

    NSString *pinIdentifier = annotation.identifier; // This is a unique string for each pin and is getting populated every time!

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];

    if(annotationView == nil)
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
    else
        annotationView.annotation = annotation; // Never had this line fire...

    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = NO;
    annotationView.enabled = YES;
    annotationView.tag = annotation.counter;

    if(annotation.pinColour == Stopped) // from enum
        annotationView.pinColor = MKPinAnnotationColorRed;
    else
        annotationView.pinColor = MKPinAnnotationColorGreen;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(mapCalloutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    infoButton.tag = annotation.counter;
    annotationView.rightCalloutAccessoryView = infoButton;

    return annotationView;
}

这是我添加图钉的代码:

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;

MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = annotationCoord;
annotation.identifier = theIdentifier;
annotation.title = theTitle;
annotation.subtitle = theSubtitle
annotation.pinColour = [self getPinColour];
annotation.counter = theCounter;

[theMapView addAnnotation:annotation];

这是我更新 pin 的代码(添加方法不同):

updatePin = true;
pinCounter = mapPin.counter;

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;
[mapPin setCoordinate:annotationCoord];

mapPin.identifier = theIdentifier;
mapPin.subtitle = theSubtitle;
mapPin.pinColour = [self getPinColour];

我不确定我错过了什么。 viewForAnnotation 显然有效,只是在初始添加后从未调用过!如果要调用这个函数,我 100% 肯定它会像我重新启动应用程序时颜色变化一样工作!

编辑:哦,我真的不想开始删除注释并重新添加它们。反正这就是我短期内要做的事情!

【问题讨论】:

    标签: ios cocoa-touch mkmapview mkannotation mkannotationview


    【解决方案1】:

    实际上,我不知道这是否适合您,但我就是这样做的。

    我不需要从地图中删除注释。我需要做的就是告诉地图给我一个参数注释的注释视图。地图将返回正确的注释。从那里,我有一个自定义注释的属性来识别它是否是活动项目,如果是,则显示正常的 pin 图像,否则显示完整的 pin 图像。

    -(void)updateAnnotationImage:(CustomAnnotation *)paramAnnotation
    {
        MKAnnotationView *av = [geoMap viewForAnnotation:paramAnnotation];
    
        if (paramAnnotation.active)
        {
            av.image = [UIImage imageNamed:@"PinNormal.png"];
        }
        else
        {
            av.image = [UIImage imageNamed:@"PinFull.png"];
        }
    }
    

    有点晚了,但希望它能帮助遇到这个问题的其他人。

    【讨论】:

    • 这是可能的,这是正确的方法。为答案+1。
    • 这确实应该是公认的答案。谢谢!
    【解决方案2】:

    由于地图视图缓存其注释的方式,如果您需要更改其外观,则需要删除并重新添加注释。一个简单的删除和添加是要走的路。除了这个,没有缓存失效机制。

    【讨论】:

    • 啊,谢谢。我真的不想那样做,但我设法保留了标注视图,所以现在你无论如何也看不出来!我会将此标记为答案,因为我不知道这是唯一的方法。
    • 这个答案有效stackoverflow.com/a/56203588/4833705。您创建一个自定义注解并将 kvo 添加到属性以在属性更改时对注解进行更改
    【解决方案3】:

    我还发现这个答案很有帮助:In which case that mapView:viewForAnnotation: will be called?

    每当你调用 addAnnotation 方法时

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation gets called. 
    

    【讨论】:

      【解决方案4】:

      斯威夫特 2.1:

      我遇到了同样的问题,找到了一个快速的解决方案,在需要时触发它,也将它发送到主线程是明智的:

      var annotationsArray = mapView.annotations
      mapView.removeAnnotations(mapView.annotations)
      mapView.addAnnotations(arrayIncs)
      arrayIncs.removeAll()
      

      【讨论】:

        【解决方案5】:

        只花了几个小时才让它在 Xamarin 上运行;这是对其他 Xamarin 开发人员的警告。确保使用 ViewForAnnotation 方法而不是 GetViewForAnnotation 委托。我使用了错误的方法,它返回了新的注释视图而不是现有的......当然它不起作用!

        【讨论】:

          猜你喜欢
          • 2012-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-11
          • 1970-01-01
          • 2019-10-26
          • 2018-12-26
          • 2018-05-08
          相关资源
          最近更新 更多