From 3bccfa9bc542ee34b141b1be2665582da347ab08 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Mon, 25 Jul 2016 11:02:45 -0700 Subject: [PATCH 1/3] refs 654f015: bring back sprite-backed annotation option for comparison --- platform/ios/app/MBXViewController.m | 106 ++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 11 deletions(-) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 5451abd5805..0b793206d35 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -184,9 +184,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", @@ -230,29 +233,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]; } @@ -276,7 +291,7 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn } } -- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount +- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount usingViews:(BOOL)useViews { [self.mapView removeAnnotations:self.mapView.annotations]; @@ -301,6 +316,7 @@ - (void)parseFeaturesAddingCount:(NSUInteger)featuresCount MGLPointAnnotation *annotation = [MGLPointAnnotation new]; annotation.coordinate = coordinate; annotation.title = title; + annotation.subtitle = [NSString stringWithFormat:@"View: %i", useViews]; [annotations addObject:annotation]; @@ -582,7 +598,8 @@ - (void)dealloc - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id)annotation { // Use GL backed pins for dropped pin annotations - if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]]) + if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || + ([annotation isKindOfClass:[MGLPointAnnotation class]] && [[(MGLPointAnnotation *)annotation subtitle] isEqualToString:@"View: 0"])) { return nil; } @@ -610,6 +627,73 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id __nonnull)annotation +{ + if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || + [annotation isKindOfClass:[MBXCustomCalloutAnnotation class]] || + ([annotation isKindOfClass:[MGLPointAnnotation class]] && [[(MGLPointAnnotation *)annotation subtitle] isEqualToString:@"View: 1"])) + { + return nil; // use default marker + } + + 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 )annotation { return YES; From 52e17c3ead91fcd0175adf8a7c0ffe5b7cbb8d96 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Mon, 25 Jul 2016 11:41:06 -0700 Subject: [PATCH 2/3] use class instead of magic string comparison --- platform/ios/app/MBXViewController.m | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 0b793206d35..5176692e1d9 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -30,6 +30,12 @@ @interface MBXCustomCalloutAnnotation : MGLPointAnnotation @implementation MBXCustomCalloutAnnotation @end +@interface MBXSpriteBackedAnnotation : MGLPointAnnotation +@end + +@implementation MBXSpriteBackedAnnotation +@end + @interface MBXViewController () @property (nonatomic) IBOutlet MGLMapView *mapView; @@ -313,10 +319,10 @@ - (void)parseFeaturesAddingCount:(NSUInteger)featuresCount usingViews:(BOOL)useV [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; - annotation.subtitle = [NSString stringWithFormat:@"View: %i", useViews]; [annotations addObject:annotation]; @@ -598,8 +604,7 @@ - (void)dealloc - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id)annotation { // Use GL backed pins for dropped pin annotations - if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || - ([annotation isKindOfClass:[MGLPointAnnotation class]] && [[(MGLPointAnnotation *)annotation subtitle] isEqualToString:@"View: 0"])) + if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || [annotation isKindOfClass:[MBXSpriteBackedAnnotation class]]) { return nil; } @@ -629,9 +634,7 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id __nonnull)annotation { - if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || - [annotation isKindOfClass:[MBXCustomCalloutAnnotation class]] || - ([annotation isKindOfClass:[MGLPointAnnotation class]] && [[(MGLPointAnnotation *)annotation subtitle] isEqualToString:@"View: 1"])) + if ([annotation isKindOfClass:[MBXDroppedPinAnnotation class]] || [annotation isKindOfClass:[MBXCustomCalloutAnnotation class]]) { return nil; // use default marker } From 38d2d7f36b42ae59ab9821d409df8b159c742676 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Mon, 25 Jul 2016 11:45:27 -0700 Subject: [PATCH 3/3] codify implicit assumption about marker-providing delegate method precedence --- platform/ios/app/MBXViewController.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 5176692e1d9..ef91c847be2 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -639,6 +639,8 @@ - (MGLAnnotationImage *)mapView:(MGLMapView * __nonnull)mapView imageForAnnotati 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];