From 02a32b55e4ec392d213d435354bbcd2f33cef40c Mon Sep 17 00:00:00 2001 From: Romain Quidet Date: Mon, 30 Nov 2015 14:16:00 +0100 Subject: [PATCH] 2210: MGLAnnotationImage image can be update --- gyp/platform-ios.gypi | 1 + include/mbgl/ios/MGLAnnotationImage.h | 4 ++-- platform/ios/MGLAnnotationImage.m | 15 ++++++++++----- platform/ios/MGLAnnotationImage_Private.h | 18 ++++++++++++++++++ platform/ios/MGLMapView.mm | 16 +++++++++++++++- 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 platform/ios/MGLAnnotationImage_Private.h diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index 9ae44b2d4..d510d25c0 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -60,6 +60,7 @@ '../platform/ios/MGLUserLocationAnnotationView.h', '../platform/ios/MGLUserLocationAnnotationView.m', '../include/mbgl/ios/MGLAnnotationImage.h', + '../platform/ios/MGLAnnotationImage_Private.h', '../platform/ios/MGLAnnotationImage.m', '../platform/ios/MGLCategoryLoader.h', '../platform/ios/MGLCategoryLoader.m', diff --git a/include/mbgl/ios/MGLAnnotationImage.h b/include/mbgl/ios/MGLAnnotationImage.h index 70bf8b759..efc79ffb9 100644 --- a/include/mbgl/ios/MGLAnnotationImage.h +++ b/include/mbgl/ios/MGLAnnotationImage.h @@ -13,12 +13,12 @@ NS_ASSUME_NONNULL_BEGIN * @param image The image to be displayed for the annotation. * @param reuseIdentifier The string that identifies that this annotation image is reusable. * @return The initialized annotation image object or `nil` if there was a problem initializing the object. */ -+ (instancetype)annotationImageWithImage:(UIImage *)image reuseIdentifier:(NSString *)reuseIdentifier; ++ (instancetype)annotationImageWithImage:(UIImage *)image reuseIdentifier:(nullable NSString *)reuseIdentifier; /** @name Getting and Setting Attributes */ /** The image to be displayed for the annotation. */ -@property (nonatomic, readonly) UIImage *image; +@property (nonatomic, strong) UIImage *image; /** The string that identifies that this annotation image is reusable. (read-only) * diff --git a/platform/ios/MGLAnnotationImage.m b/platform/ios/MGLAnnotationImage.m index cd211c52e..ad560552c 100644 --- a/platform/ios/MGLAnnotationImage.m +++ b/platform/ios/MGLAnnotationImage.m @@ -1,20 +1,20 @@ -#import "MGLAnnotationImage.h" +#import "MGLAnnotationImage_Private.h" @interface MGLAnnotationImage () -@property (nonatomic) UIImage *image; -@property (nonatomic) NSString *reuseIdentifier; +@property (nonatomic, strong) NSString *reuseIdentifier; +@property (nonatomic, weak) id delegate; @end @implementation MGLAnnotationImage -+ (instancetype)annotationImageWithImage:(UIImage *)image reuseIdentifier:(NSString *)reuseIdentifier ++ (instancetype)annotationImageWithImage:(UIImage *)image reuseIdentifier:(nullable NSString *)reuseIdentifier { return [[self alloc] initWithImage:image reuseIdentifier:reuseIdentifier]; } -- (instancetype)initWithImage:(UIImage *)image reuseIdentifier:(NSString *)reuseIdentifier +- (instancetype)initWithImage:(UIImage *)image reuseIdentifier:(nullable NSString *)reuseIdentifier { self = [super init]; @@ -28,4 +28,9 @@ return self; } +- (void)setImage:(UIImage *)image { + _image = image; + [self.delegate annotationImageNeedsRedisplay:self]; +} + @end diff --git a/platform/ios/MGLAnnotationImage_Private.h b/platform/ios/MGLAnnotationImage_Private.h new file mode 100644 index 000000000..f22a9ac4e --- /dev/null +++ b/platform/ios/MGLAnnotationImage_Private.h @@ -0,0 +1,18 @@ +#import "MGLAnnotationImage.h" + +NS_ASSUME_NONNULL_BEGIN + +@protocol MGLAnnotationImageDelegate + +@required +- (void)annotationImageNeedsRedisplay:(MGLAnnotationImage *)annotationImage; + +@end + +@interface MGLAnnotationImage (Private) + +@property (nonatomic, weak) id delegate; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 9050773a8..fc4a655e6 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -36,6 +36,7 @@ #import "MGLUserLocationAnnotationView.h" #import "MGLUserLocation_Private.h" #import "MGLAccountManager_Private.h" +#import "MGLAnnotationImage_Private.h" #import "MGLMapboxEvents.h" #import "SMCalloutView.h" @@ -120,7 +121,8 @@ public: CLLocationManagerDelegate, UIActionSheetDelegate, SMCalloutViewDelegate, - MGLMultiPointDelegate> + MGLMultiPointDelegate, + MGLAnnotationImageDelegate> @property (nonatomic) EAGLContext *context; @property (nonatomic) GLKView *glView; @@ -2166,6 +2168,7 @@ std::chrono::steady_clock::duration MGLDurationInSeconds(float duration) { self.annotationImagesByIdentifier[annotationImage.reuseIdentifier] = annotationImage; [self installAnnotationImage:annotationImage]; + annotationImage.delegate = self; } NSString *symbolName = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier]; @@ -2709,6 +2712,17 @@ std::chrono::steady_clock::duration MGLDurationInSeconds(float duration) animated:animated]; } +#pragma mark Annotation Image Delegate + +- (void)annotationImageNeedsRedisplay:(MGLAnnotationImage *)annotationImage +{ + // remove sprite + NSString *symbolName = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier]; + _mbglMap->removeSprite(symbolName.UTF8String); + [self installAnnotationImage:annotationImage]; + _mbglMap->update(mbgl::Update::Annotations); +} + #pragma mark - User Location - - (void)setShowsUserLocation:(BOOL)showsUserLocation