【问题标题】:Custom MKAnnotationView Showing Wrong Image自定义 MKAnnotationView 显示错误的图像
【发布时间】:2015-01-14 11:35:42
【问题描述】:

我有一个MKMapView,我想在其中显示联系人面孔的注释。我查看了 Apple 的示例应用程序 MapCallouts 并到目前为止写了这个:

Annotation.h

@interface Annotation : NSObject <MKAnnotation>

@property (nonatomic, strong) UIImage *image;
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

+ (MKAnnotationView *)createViewAnnotationForMapView:(MKMapView *)mapView annotation:(id <MKAnnotation>)annotation;

@end

Annotation.m

#import "CustomAnnotationView.h"

@implementation Annotation

+ (MKAnnotationView *)createViewAnnotationForMapView:(MKMapView *)mapView annotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *returnedAnnotationView =
    (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Annotation"];
    if (returnedAnnotationView == nil)
    {
        returnedAnnotationView =
        [[CustomAnnotationView alloc] initWithAnnotation:annotation
                                         reuseIdentifier:@"Annotation"];
    }

    return returnedAnnotationView;
}

@end

CustomAnnotationView.h

@interface CustomAnnotationView : MKAnnotationView

CustomAnnotationView.m

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{

    self = [super initWithAnnotation:annotation reuseIdentifier:@"Annotation"];
    if (self != nil)
    {
        Annotation *mapItem = (Annotation *)self.annotation;

        // Create imageView and labels, etc.
    }

    return self;
}

然后,为了删除我的注释,我有这个方法循环遍历一组联系人,获取他们的地址,找到 CLLOcationCoordinate2D,并创建一个Annotation

-(void)sortContacts{

    for (APContact *contact in self.peopleArray){

          [operationQueue addOperationWithBlock:^{

            NSString *addressString = // get address from contact;

            contact.addressString = addressString;

                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
                [geocoder geocodeAddressString:addressString
                             completionHandler:^(NSArray* placemarks, NSError* error){
                                 if (placemarks && placemarks.count > 0 && !error) {

                                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                                     CLLocation *location = topResult.location;
                                     CLLocationCoordinate2D coordinate = location.coordinate;

                                         Annotation *annotation = [[Annotation alloc] init];
                                         annotation.image = [UIImage imageNamed:@"Annotation"];

                                         if (contact.thumbnail.size.width > 0){
                                            annotation.image = contact.thumbnail;
                                         }
                                         else{
                                            annotation.image = [UIImage imageNamed:@"Annotation"];
                                         }

                                         annotation.coordinate = coordinate;
                                         [self.mapView addAnnotation:annotation];
                                 }
                                 else if (error){
                                     NSLog(@"ERROR");
                                 }
                            }
                 ];
        }];
    }
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    CustomAnnotationView *returnedAnnotationView = nil;

           if ([annotation isKindOfClass:[Annotation class]])
        {
            returnedAnnotationView = (CustomAnnotationView *)[Annotation createViewAnnotationForMapView:self.mapView annotation:annotation];
        }

    return returnedAnnotationView;
}

但是,当我滚动浏览 self.mapView 时,会显示错误的联系人图像以显示错误的注释。我不知道如何解决这个问题。任何帮助将非常感激。谢谢

【问题讨论】:

  • "然后,要删除我的注释,我有这个方法,它循环遍历一组联系人,获取他们的地址,找到 CLLOcationCoordinate2D,然后创建一个..." 并创建一个什么?跨度>
  • @LyndseyScott 抱歉,忘记添加“注释”。 :)
  • 看起来像注释视图重用问题。在 createViewAnnotationForMapView 中,如果视图出队,则不会更新其注释和图像(因此会显示该视图用于的先前注释的图像)。
  • @Anna Aha,我不知道为什么我没想到! (尤其是在最近使用 UITableView 之后)。您的评论帮助我想到了答案,我在下面回答了这个问题。谢谢:)

标签: ios objective-c cocoa-touch mkmapview


【解决方案1】:

我在我的自定义注释视图中添加了一个属性,并在每次我出列或创建新注释时简单地更改图像。

@property (nonatomic, strong) UIImageView *annotationImage;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多