Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit d9a1033

Browse files
authored
[ios] Use transparent image for annotations backed by views (#5461)
The current implementation of view backed annotations mostly relies on the existing annotation model manager and the associated underlying boost query implementations for annotation management. However, since the representation of the model is a view, an image was not installed for view backed annotations. Because the underlying annotation management implementation is not entire comfortable working without an image, it would report `[INFO] {Worker}[Sprite]: Can't find sprite named 'default_marker'` when annotation models without an image were encountered. This updates the annotation adding logic in to create (or reuse) and install a small, invisible image for each view backed annotation.
1 parent d29bef9 commit d9a1033

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

platform/ios/src/MGLMapView.mm

+32-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ typedef NS_ENUM(NSUInteger, MGLUserTrackingState) {
8888
/// Reuse identifier and file name of the default point annotation image.
8989
static NSString * const MGLDefaultStyleMarkerSymbolName = @"default_marker";
9090

91+
/// Reuse identifier and file name of the invisible point annotation image used
92+
/// by annotations that are visually backed by MGLAnnotationView objects
93+
static NSString * const MGLInvisibleStyleMarkerSymbolName = @"invisible_marker";
94+
9195
/// Prefix that denotes a sprite installed by MGLMapView, to avoid collisions
9296
/// with style-defined sprites.
9397
NSString *const MGLAnnotationSpritePrefix = @"com.mapbox.sprites.";
@@ -2861,6 +2865,14 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
28612865
annotationView.annotation = annotation;
28622866
annotationView.center = [self convertCoordinate:annotation.coordinate toPointToView:self];
28632867
[newAnnotationViews addObject:annotationView];
2868+
2869+
MGLAnnotationImage *annotationImage = self.invisibleAnnotationImage;
2870+
symbolName = annotationImage.styleIconIdentifier;
2871+
annotationImagesForAnnotation[annotationValue] = annotationImage;
2872+
if ( ! self.annotationImagesByIdentifier[annotationImage.reuseIdentifier])
2873+
{
2874+
[self installAnnotationImage:annotationImage];
2875+
}
28642876
}
28652877
}
28662878

@@ -2897,22 +2909,21 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
28972909

28982910
MGLAnnotationTag annotationTag = _mbglMap->addAnnotation(mbgl::SymbolAnnotation {
28992911
MGLPointFromLocationCoordinate2D(annotation.coordinate),
2900-
symbolName.UTF8String ?: ""
2912+
symbolName.UTF8String
29012913
});
29022914

29032915
MGLAnnotationContext context;
29042916
context.annotation = annotation;
29052917
MGLAnnotationImage *annotationImage = annotationImagesForAnnotation[annotationValue];
2918+
context.imageReuseIdentifier = annotationImage.reuseIdentifier;
29062919

2907-
if (annotationImage) {
2908-
context.imageReuseIdentifier = annotationImage.reuseIdentifier;
2909-
}
29102920
if (annotationView) {
29112921
context.annotationView = annotationView;
29122922
context.viewReuseIdentifier = annotationView.reuseIdentifier;
29132923
}
29142924

29152925
_annotationContextsByAnnotationTag[annotationTag] = context;
2926+
29162927
if ([annotation isKindOfClass:[NSObject class]]) {
29172928
NSAssert(![annotation isKindOfClass:[MGLMultiPoint class]], @"Point annotation should not be MGLMultiPoint.");
29182929
[(NSObject *)annotation addObserver:self forKeyPath:@"coordinate" options:0 context:(void *)(NSUInteger)annotationTag];
@@ -2962,6 +2973,23 @@ - (MGLAnnotationImage *)defaultAnnotationImage
29622973
return annotationImage;
29632974
}
29642975

2976+
- (MGLAnnotationImage *)invisibleAnnotationImage
2977+
{
2978+
MGLAnnotationImage *annotationImage = [self dequeueReusableAnnotationImageWithIdentifier:MGLInvisibleStyleMarkerSymbolName];
2979+
2980+
if (!annotationImage)
2981+
{
2982+
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
2983+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
2984+
UIGraphicsEndImageContext();
2985+
annotationImage = [MGLAnnotationImage annotationImageWithImage:image
2986+
reuseIdentifier:MGLInvisibleStyleMarkerSymbolName];
2987+
annotationImage.styleIconIdentifier = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier];
2988+
}
2989+
2990+
return annotationImage;
2991+
}
2992+
29652993
- (MGLAnnotationView *)annotationViewForAnnotation:(id<MGLAnnotation>)annotation
29662994
{
29672995
MGLAnnotationView *annotationView = [self.delegate mapView:self viewForAnnotation:annotation];

0 commit comments

Comments
 (0)