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

Commit

Permalink
refs 654f015: bring back sprite-backed annotation option for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Jul 26, 2016
1 parent 0d7d871 commit de197ca
Showing 1 changed file with 95 additions and 11 deletions.
106 changes: 95 additions & 11 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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];
}
Expand All @@ -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];

Expand All @@ -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];

Expand Down Expand Up @@ -582,7 +598,8 @@ - (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:[MGLPointAnnotation class]] && [[(MGLPointAnnotation *)annotation subtitle] isEqualToString:@"View: 0"]))
{
return nil;
}
Expand Down Expand Up @@ -610,6 +627,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]] ||
([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 <MGLAnnotation>)annotation
{
return YES;
Expand Down

0 comments on commit de197ca

Please sign in to comment.