Skip to content

Instantly share code, notes, and snippets.

@christopherdro
Created January 6, 2016 22:04
Show Gist options
  • Save christopherdro/f245ca06e21dd9fb554c to your computer and use it in GitHub Desktop.
Save christopherdro/f245ca06e21dd9fb554c to your computer and use it in GitHub Desktop.
NSObject *convertObjectToPoint (NSObject *annotationObject)
{
NSString *title = @"";
if ([annotationObject valueForKey:@"title"]) {
title = [RCTConvert NSString:[annotationObject valueForKey:@"title"]];
}
NSString *subtitle = @"";
if ([annotationObject valueForKey:@"subtitle"]) {
subtitle = [RCTConvert NSString:[annotationObject valueForKey:@"subtitle"]];
}
NSString *id = @"";
if ([annotationObject valueForKey:@"id"]) {
id = [RCTConvert NSString:[annotationObject valueForKey:@"id"]];
}
if ([annotationObject valueForKey:@"rightCalloutAccessory"]) {
NSObject *rightCalloutAccessory = [annotationObject valueForKey:@"rightCalloutAccessory"];
NSString *url = [rightCalloutAccessory valueForKey:@"url"];
CGFloat height = (CGFloat)[[rightCalloutAccessory valueForKey:@"height"] floatValue];
CGFloat width = (CGFloat)[[rightCalloutAccessory valueForKey:@"width"] floatValue];
UIImage *image = nil;
if ([url hasPrefix:@"image!"]) {
NSString* localImagePath = [url substringFromIndex:6];
image = [UIImage imageNamed:localImagePath];
}
NSURL* checkURL = [NSURL URLWithString:url];
if (checkURL && checkURL.scheme && checkURL.host) {
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
}
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(0, 0, height, width);
[imageButton setImage:image forState:UIControlStateNormal];
NSArray *coordinate = [RCTConvert NSArray:[annotationObject valueForKey:@"coordinates"]];
CLLocationDegrees lat = [coordinate[0] doubleValue];
CLLocationDegrees lng = [coordinate[1] doubleValue];
RCTMGLAnnotation *pin = [[RCTMGLAnnotation alloc] initWithLocationRightCallout:CLLocationCoordinate2DMake(lat, lng) title:title subtitle:subtitle id:id rightCalloutAccessory:imageButton];
if ([annotationObject valueForKey:@"annotationImage"]) {
NSObject *annotationImage = [annotationObject valueForKey:@"annotationImage"];
NSString *annotationImageURL = [annotationImage valueForKey:@"url"];
CGFloat height = (CGFloat)[[annotationImage valueForKey:@"height"] floatValue];
CGFloat width = (CGFloat)[[annotationImage valueForKey:@"width"] floatValue];
if (!height || !width) {
RCTLogError(@"Height and width for image required");
return nil;
}
CGSize annotationImageSize = CGSizeMake(width, height);
pin.annotationImageURL = annotationImageURL;
pin.annotationImageSize = annotationImageSize;
}
return pin;
} else {
NSArray *coordinate = [RCTConvert NSArray:[annotationObject valueForKey:@"coordinates"]];
CLLocationDegrees lat = [coordinate[0] doubleValue];
CLLocationDegrees lng = [coordinate[1] doubleValue];
RCTMGLAnnotation *point = [[RCTMGLAnnotation alloc] initWithLocation:CLLocationCoordinate2DMake(lat, lng) title:title subtitle:subtitle id:id];
if ([annotationObject valueForKey:@"annotationImage"]) {
NSObject *annotationImage = [annotationObject valueForKey:@"annotationImage"];
NSString *annotationImageURL = [annotationImage valueForKey:@"url"];
CGFloat height = (CGFloat)[[annotationImage valueForKey:@"height"] floatValue];
CGFloat width = (CGFloat)[[annotationImage valueForKey:@"width"] floatValue];
if (!height || !width) {
RCTLogError(@"Height and width for image required");
return nil;
}
CGSize annotationImageSize = CGSizeMake(width, height);
point.annotationImageURL = annotationImageURL;
point.annotationImageSize = annotationImageSize;
}
return point;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment