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

Commit

Permalink
[ios] Use transparent image for annotations backed by views
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
boundsj committed Jun 24, 2016
1 parent d29bef9 commit cae73ac
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ typedef NS_ENUM(NSUInteger, MGLUserTrackingState) {
/// Reuse identifier and file name of the default point annotation image.
static NSString * const MGLDefaultStyleMarkerSymbolName = @"default_marker";

/// Reuse identifier and file name of the invisible point annotation image used
/// by annotations that are visually backed by MGLAnnotationView objects
static NSString * const MGLInvisibleStyleMarkerSymbolName = @"invisible_marker";

/// Prefix that denotes a sprite installed by MGLMapView, to avoid collisions
/// with style-defined sprites.
NSString *const MGLAnnotationSpritePrefix = @"com.mapbox.sprites.";
Expand Down Expand Up @@ -2861,6 +2865,14 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
annotationView.annotation = annotation;
annotationView.center = [self convertCoordinate:annotation.coordinate toPointToView:self];
[newAnnotationViews addObject:annotationView];

MGLAnnotationImage *annotationImage = self.invisibleAnnotationImage;
symbolName = annotationImage.styleIconIdentifier;
annotationImagesForAnnotation[annotationValue] = annotationImage;
if ( ! self.annotationImagesByIdentifier[annotationImage.reuseIdentifier])
{
[self installAnnotationImage:annotationImage];
}
}
}

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

MGLAnnotationTag annotationTag = _mbglMap->addAnnotation(mbgl::SymbolAnnotation {
MGLPointFromLocationCoordinate2D(annotation.coordinate),
symbolName.UTF8String ?: ""
symbolName.UTF8String
});

MGLAnnotationContext context;
context.annotation = annotation;
MGLAnnotationImage *annotationImage = annotationImagesForAnnotation[annotationValue];
context.imageReuseIdentifier = annotationImage.reuseIdentifier;

if (annotationImage) {
context.imageReuseIdentifier = annotationImage.reuseIdentifier;
}
if (annotationView) {
context.annotationView = annotationView;
context.viewReuseIdentifier = annotationView.reuseIdentifier;
}

_annotationContextsByAnnotationTag[annotationTag] = context;

if ([annotation isKindOfClass:[NSObject class]]) {
NSAssert(![annotation isKindOfClass:[MGLMultiPoint class]], @"Point annotation should not be MGLMultiPoint.");
[(NSObject *)annotation addObserver:self forKeyPath:@"coordinate" options:0 context:(void *)(NSUInteger)annotationTag];
Expand Down Expand Up @@ -2962,6 +2973,23 @@ - (MGLAnnotationImage *)defaultAnnotationImage
return annotationImage;
}

- (MGLAnnotationImage *)invisibleAnnotationImage
{
MGLAnnotationImage *annotationImage = [self dequeueReusableAnnotationImageWithIdentifier:MGLInvisibleStyleMarkerSymbolName];

if (!annotationImage)
{
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationImage = [MGLAnnotationImage annotationImageWithImage:image
reuseIdentifier:MGLInvisibleStyleMarkerSymbolName];
annotationImage.styleIconIdentifier = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier];
}

return annotationImage;
}

- (MGLAnnotationView *)annotationViewForAnnotation:(id<MGLAnnotation>)annotation
{
MGLAnnotationView *annotationView = [self.delegate mapView:self viewForAnnotation:annotation];
Expand Down

0 comments on commit cae73ac

Please sign in to comment.