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

refs 654f015: bring back sprite-backed annotation option for comparison #5786

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 101 additions & 12 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ @interface MBXCustomCalloutAnnotation : MGLPointAnnotation
@implementation MBXCustomCalloutAnnotation
@end

@interface MBXSpriteBackedAnnotation : MGLPointAnnotation
@end

@implementation MBXSpriteBackedAnnotation
@end

@interface MBXViewController () <UIActionSheetDelegate, MGLMapViewDelegate>

@property (nonatomic) IBOutlet MGLMapView *mapView;
Expand Down Expand Up @@ -184,9 +190,12 @@ - (IBAction)showSettings:(__unused id)sender
((debugMask & MGLMapDebugOverdrawVisualizationMask)
? @"Hide Overdraw Visualization"
: @"Show Overdraw Visualization"),
@"Add 100 Points",
@"Add 1,000 Points",
@"Add 10,000 Points",
@"Add 100 Views",
@"Add 1,000 Views",
@"Add 10,000 Views",
@"Add 100 Sprites",
@"Add 1,000 Sprites",
@"Add 10,000 Sprites",
@"Add Test Shapes",
@"Start World Tour",
@"Add Custom Callout Point",
Expand Down Expand Up @@ -230,29 +239,41 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 6)
{
[self parseFeaturesAddingCount:100];
[self parseFeaturesAddingCount:100 usingViews:YES];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 7)
{
[self parseFeaturesAddingCount:1000];
[self parseFeaturesAddingCount:1000 usingViews:YES];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 8)
{
[self parseFeaturesAddingCount:10000];
[self parseFeaturesAddingCount:10000 usingViews:YES];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 9)
{
[self addTestShapes];
[self parseFeaturesAddingCount:100 usingViews:NO];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 10)
{
[self startWorldTour:actionSheet];
[self parseFeaturesAddingCount:1000 usingViews:NO];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 11)
{
[self presentAnnotationWithCustomCallout];
[self parseFeaturesAddingCount:10000 usingViews:NO];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 12)
{
[self addTestShapes];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 13)
{
[self startWorldTour:actionSheet];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 14)
{
[self presentAnnotationWithCustomCallout];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 15)
{
[self.mapView removeAnnotations:self.mapView.annotations];
}
Expand All @@ -276,7 +297,7 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
}
}

- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount
- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount usingViews:(BOOL)useViews
{
[self.mapView removeAnnotations:self.mapView.annotations];

Expand All @@ -298,7 +319,8 @@ - (void)parseFeaturesAddingCount:(NSUInteger)featuresCount
[feature[@"geometry"][@"coordinates"][0] doubleValue]);
NSString *title = feature[@"properties"][@"NAME"];

MGLPointAnnotation *annotation = [MGLPointAnnotation new];
MGLPointAnnotation *annotation = (useViews ? [MGLPointAnnotation new] : [MBXSpriteBackedAnnotation new]);

annotation.coordinate = coordinate;
annotation.title = title;

Expand Down Expand Up @@ -582,7 +604,7 @@ - (void)dealloc
- (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAnnotation>)annotation
{
// Use GL backed pins for dropped pin annotations
if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]])
if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || [annotation isKindOfClass:[MBXSpriteBackedAnnotation class]])
{
return nil;
}
Expand Down Expand Up @@ -610,6 +632,73 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAn
return annotationView;
}

- (MGLAnnotationImage *)mapView:(MGLMapView * __nonnull)mapView imageForAnnotation:(id <MGLAnnotation> __nonnull)annotation
{
if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || [annotation isKindOfClass:[MBXCustomCalloutAnnotation class]])
{
return nil; // use default marker
}

NSAssert([annotation isKindOfClass:[MBXSpriteBackedAnnotation class]], @"Annotations should be sprite-backed.");

NSString *title = [(MGLPointAnnotation *)annotation title];
if (!title.length) return nil;
NSString *lastTwoCharacters = [title substringFromIndex:title.length - 2];

MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:lastTwoCharacters];

if ( ! annotationImage)
{
UIColor *color;

// make every tenth annotation blue
if ([lastTwoCharacters hasSuffix:@"0"]) {
color = [UIColor blueColor];
} else {
color = [UIColor redColor];
}

UIImage *image = [self imageWithText:lastTwoCharacters backgroundColor:color];
annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:lastTwoCharacters];

// don't allow touches on blue annotations
if ([color isEqual:[UIColor blueColor]]) annotationImage.enabled = NO;
}

return annotationImage;
}


- (UIImage *)imageWithText:(NSString *)text backgroundColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 20, 15);

UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(ctx, [[color colorWithAlphaComponent:0.75] CGColor]);
CGContextFillRect(ctx, rect);

CGContextSetStrokeColorWithColor(ctx, [[UIColor blackColor] CGColor]);
CGContextStrokeRectWithWidth(ctx, rect, 2);

NSAttributedString *drawString = [[NSAttributedString alloc] initWithString:text attributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Arial-BoldMT" size:12],
NSForegroundColorAttributeName: [UIColor whiteColor],
}];
CGSize stringSize = drawString.size;
CGRect stringRect = CGRectMake((rect.size.width - stringSize.width) / 2,
(rect.size.height - stringSize.height) / 2,
stringSize.width,
stringSize.height);
[drawString drawInRect:stringRect];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

- (BOOL)mapView:(__unused MGLMapView *)mapView annotationCanShowCallout:(__unused id <MGLAnnotation>)annotation
{
return YES;
Expand Down