自定义MKAnnotationView显示错误的图像

时间:2015-01-14 11:35:43

标签: ios objective-c cocoa-touch mkmapview

我有一个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时,错误的注释会显示错误的联系人图片。我不知道如何解决这个问题。任何帮助将非常感激。感谢

1 个答案:

答案 0 :(得分:0)

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

@property (nonatomic, strong) UIImageView *annotationImage;