【问题标题】:Custom annotation won't update on the map view when it's updated自定义注释在更新时不会在地图视图上更新
【发布时间】:2013-11-18 04:37:02
【问题描述】:

我正在进行willChangeValueForKeydidChangeValueForKey 调用,但即使使用它们,mapView 委托也不会通过mapView: viewForAnnotation: 来更新事物。如何强制更新MKAnnotation

annotationView.image 不会在图像更改时更新,annotationView.image 也不会更新。据我的 NSLog 行所知,mapView: viewForAnnotation: 在更新 MKAnnotation 时不会再次被调用。

.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *currentTitle;
@property (nonatomic, retain) NSString *currentSubTitle;
@property (nonatomic, retain) NSString *category;
@property (nonatomic, retain) NSString *pin;

- (NSString*) title;
- (NSString*) subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) c;
- (CLLocationCoordinate2D) getCoordinate;
- (void) addPlace:(MapAnnotation*) place;
- (int) placesCount;
- (void) cleanPlaces;
- (UIImage *) getPin;
@end

.m

#import "MapAnnotation.h"

@interface MapAnnotation()
@property (strong) NSMutableArray *places;
@end

@implementation MapAnnotation

@synthesize coordinate      = _coordinate;
@synthesize currentTitle    = _currentTitle;
@synthesize currentSubTitle = _currentSubTitle;
@synthesize places          = _places;
@synthesize category        = _category;
@synthesize pin             = _pin;


- (NSString*) subtitle {
    if ([self placesCount] == 1) {
        return self.currentSubTitle;
    }
    else{
        return @"";
    }
}


- (NSString*) title {
    if ([self placesCount] == 1) {
        return self.currentTitle;
    }
    else{
        return [NSString stringWithFormat:@"%d places", [self placesCount]];
    }
}


- (void)addPlace:(MapAnnotation *)place {
    [self willChangeValueForKey:@"title"];
    [self willChangeValueForKey:@"subtitle"];
    [self.places addObject:place];
    [self didChangeValueForKey:@"title"];
    [self didChangeValueForKey:@"subtitle"];
}


- (CLLocationCoordinate2D)getCoordinate {
    return self.coordinate;
}


- (void)cleanPlaces {
    [self willChangeValueForKey:@"title"];
    [self willChangeValueForKey:@"subtitle"];
    [self.places removeAllObjects];
    [self.places addObject:self];
    [self didChangeValueForKey:@"title"];
    [self didChangeValueForKey:@"subtitle"];
}


- (id)initWithCoordinate:(CLLocationCoordinate2D) c {
    self.coordinate=c;
    self.places=[[NSMutableArray alloc] initWithCapacity:0];
    return self;
}


- (int)placesCount {
    return [self.places count];
}


- (UIImage *)getPin {
    // begin a graphics context of sufficient size
    CGSize size = CGSizeMake(26, 26);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

    // get the context for CoreGraphics
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGRect imageRect = CGRectMake(0, 0, size.width, size.height);

    // Draw Dot
    CGRect circleRect = CGRectInset(imageRect, 4, 4);
    [[UIColor blackColor] setStroke];
    [[UIColor yellowColor] setFill];
    CGContextFillEllipseInRect(ctx, circleRect);
    CGContextStrokeEllipseInRect(ctx, circleRect);

    // Dot Content
    [[UIColor blackColor] setStroke];
    [[UIColor blackColor] setFill];
    CGAffineTransform transform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(ctx, transform);
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetCharacterSpacing(ctx, 1.7);
    CGContextSetTextDrawingMode(ctx, kCGTextFill);
    UIFont *font = [UIFont fontWithName:@"Arial" size:11.0];
    if ([self placesCount] != 1) {
        NSString *label = [NSString stringWithFormat:@"%d", [self placesCount]];
        CGSize stringSize = [label sizeWithAttributes:@{NSFontAttributeName:font}];
        [label drawAtPoint:CGPointMake(size.width / 2 - stringSize.width / 2, size.height / 2 - stringSize.height / 2)
            withAttributes:@{NSFontAttributeName:font}];
    } else {
        NSString *label = [NSString stringWithFormat:@"%@", self.pin];
        CGSize stringSize = [label sizeWithAttributes:@{NSFontAttributeName:font}];
        [label drawAtPoint:CGPointMake(size.width / 2 - stringSize.width / 2, size.height / 2 - stringSize.height / 2)
            withAttributes:@{NSFontAttributeName:font}];
    }


    // make image out of bitmap context
    UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

    // free the context
    UIGraphicsEndImageContext();

    return retImage;
}

@end

ma​​pView:viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnoation"];
    if(!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyAnnoation"];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }

    annotationView.enabled = YES;
    if ([(MapAnnotation*)annotation placesCount] == 1) {
        annotationView.canShowCallout = YES;
    }
    annotationView.image = [(MapAnnotation*)annotation getPin];
    NSLog(@"%@", [annotation title]);

    return annotationView;
}

【问题讨论】:

  • 只有注释视图的 callout 会自动观察标题和副标题的变化。视图本身(包括图像)不会观察这些属性,也不会自动更新视图。您是否使用自定义注释视图(以及自定义注释)?视图是否观察到标题和副标题的变化?显示 viewForAnnotation 方法。或者,您可以在更新注释时手动更新视图。
  • @AnnaKarenina - 目前没有自定义视图。我的问题是我正在更改注释图标,而不是更新。我将添加更多代码。

标签: ios mkmapview mkannotation


【解决方案1】:

正如评论中提到的,除了titlesubtitle,注释属性的更改不会自动导致视图更新。

更改 titlesubtitle 将在显示时自动更新标注,只要更改导致 KVO 通知(例如,如果使用带有 ann.title = xxx 的默认设置器或通过手动发出 KVO 通知作为问题中的代码正在执行,因为您有自定义 getter)。

要自动更新图像,您可能需要使用自定义注释视图来观察底层注释属性的变化并自行刷新(在init 中调用addObserver,在observeValueForKeyPath 中更新图像,在@ 987654329@ 致电removeObserver)。您也可以使用非 KVO 方法,例如自定义通知或委托(但仍使用自定义注释视图)来实现。

使用现有代码,在不创建自定义注释视图和使用KVO等的情况下,快速手动更新注释视图的方法是在注释更新后立即获取注释的当前视图并更新image属性.只要viewForAnnotation 中的代码与设置image 的逻辑相同,这应该可以工作。

我假设在应用程序的某个地方(有时注释已经添加之后),它通过调用addPlace 来更新注释中的位置。之后,您可以尝试以下操作:

[someExistingAnnotation addPlace:anotherPlace];

//obtain the current view of someExistingAnnotation...
MKAnnotationView *av = [mapView viewForAnnotation:someExistingAnnotation];

//update its image property to force refresh...
av.image = [someExistingAnnotation getPin];
//also set canShowCallout (it might have been NO)


不相关但当前的viewForAnnotation 不能正确处理可能的视图重用和/或除您的自定义注释之外的注释类型(例如会导致当前代码崩溃的用户位置蓝点)。我建议将其更改为:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (! [annotation isKindOfClass:[MapAnnotation class]])
    {
        //annotation is NOT a "MapAnnotation", return nil for default view...
        return nil;
    }

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnoation"];
    if(!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyAnnoation"];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }

    //update view's annotation to current in case it's being re-used
    annotationView.annotation = annotation;

    annotationView.enabled = YES;
    if ([(MapAnnotation*)annotation placesCount] == 1) {
        annotationView.canShowCallout = YES;
    }
    else {
        annotationView.canShowCallout = NO;
    }

    annotationView.image = [(MapAnnotation*)annotation getPin];
    NSLog(@"%@", [annotation title]);

    return annotationView;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多