Authored by Justin R. Miller

fleshed out success/failure callbacks for static map retrieval

@@ -27,14 +27,36 @@ @@ -27,14 +27,36 @@
27 27
28 #import <UIKit/UIKit.h> 28 #import <UIKit/UIKit.h>
29 29
  30 +/** An RMStaticMapView object provides an embeddable, static map image view. You use this class to display map information in your application that does not need to change or provide user interaction. You can center the map on a given coordinate and zoom level, specify the size of the area you want to display, and optionally provide callbacks that can be performed on map image retrieval success or failure.
  31 + *
  32 + * @warning Please note that you are responsible for getting permission to use the map data, and for ensuring your use adheres to the relevant terms of use. */
30 @interface RMStaticMapView : UIImageView 33 @interface RMStaticMapView : UIImageView
31 34
32 -- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)initialZoomLevel; 35 +/** @name Initializing a Static Map View */
  36 +
  37 +/** Initialize a static map view with a given frame, mapID, center coordinate, and zoom level.
  38 + * @param frame The frame with which to initialize the map view.
  39 + * @param mapID The MapBox map ID string, typically in the format `<username>.map-<random characters>`.
  40 + * @param centerCoordinate The map center coordinate.
  41 + * @param zoomLevel The map zoom level.
  42 + * @return An initialized map view, or `nil` if the map view was unable to be initialized. */
  43 +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel;
  44 +
  45 +/** Initialize a static map view with a given frame, mapID, center coordinate, and zoom level, performing success or failure callbacks based on retrieval of the map image.
  46 + * @param frame The frame with which to initialize the map view.
  47 + * @param mapID The MapBox map ID string, typically in the format `<username>.map-<random characters>`.
  48 + * @param centerCoordinate The map center coordinate.
  49 + * @param zoomLevel The map zoom level.
  50 + * @param successBlock A block to be performed upon map image retrieval success. The map image is passed as an argument to the block in the event that you wish to use it elsewhere or modify it.
  51 + * @param failureBlock A block to be performed upon map image retrieval failure. The retrieval error is passed as an argument to the block.
  52 + * @return An initialized map view, or `nil` if the map view was unable to be initialized. */
  53 +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel success:(void (^)(UIImage *))successBlock failure:(void (^)(NSError *))failureBlock;
  54 +
  55 +/** @name Fine-Tuning the Map Appearance */
33 56
34 /** A Boolean value indicating whether to show a small logo in the corner of the map view. Defaults to `YES`. */ 57 /** A Boolean value indicating whether to show a small logo in the corner of the map view. Defaults to `YES`. */
35 @property (nonatomic, assign) BOOL showLogoBug; 58 @property (nonatomic, assign) BOOL showLogoBug;
36 59
37 -// TODO: success/failure blocks  
38 // TODO: markers 60 // TODO: markers
39 // TODO: attribution 61 // TODO: attribution
40 62
@@ -36,67 +36,106 @@ @@ -36,67 +36,106 @@
36 { 36 {
37 UIImageView *_logoBug; 37 UIImageView *_logoBug;
38 UIActivityIndicatorView *_spinner; 38 UIActivityIndicatorView *_spinner;
  39 + NSURL *_requestURL;
39 } 40 }
40 41
41 @synthesize showLogoBug=_showLogoBug; 42 @synthesize showLogoBug=_showLogoBug;
42 43
43 -- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)initialZoomLevel 44 +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
44 { 45 {
45 if (!(self = [super initWithFrame:frame])) 46 if (!(self = [super initWithFrame:frame]))
46 return nil; 47 return nil;
47 48
48 - CGRect requestFrame = CGRectMake(frame.origin.x, frame.origin.y, fminf(frame.size.width, RMStaticMapViewMaxWidth), fminf(frame.size.height, RMStaticMapViewMaxHeight));  
49 -  
50 - if ( ! CLLocationCoordinate2DIsValid(centerCoordinate))  
51 - centerCoordinate = CLLocationCoordinate2DMake(0, 0);  
52 -  
53 - initialZoomLevel = fmaxf(initialZoomLevel, RMStaticMapViewMinZoom);  
54 - initialZoomLevel = fminf(initialZoomLevel, RMStaticMapViewMaxZoom);  
55 -  
56 - self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];  
57 -  
58 - self.contentMode = UIViewContentModeCenter; 49 + [self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
59 50
60 - self.showLogoBug = YES;  
61 -  
62 - _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];  
63 -  
64 - [_spinner startAnimating]; 51 + [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
  52 + queue:[NSOperationQueue new]
  53 + completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
  54 + {
  55 + [_spinner removeFromSuperview];
  56 + [_spinner release]; _spinner = nil;
65 57
66 - _spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 58 + if (responseData)
  59 + {
  60 + dispatch_async(dispatch_get_main_queue(), ^(void)
  61 + {
  62 + self.image = [UIImage imageWithData:responseData];
  63 + });
  64 + }
  65 + }];
67 66
68 - _spinner.center = self.center; 67 + return self;
  68 +}
69 69
70 - [self addSubview:_spinner]; 70 +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel success:(void (^)(UIImage *))successBlock failure:(void (^)(NSError *))failureBlock
  71 +{
  72 + if (!(self = [super initWithFrame:frame]))
  73 + return nil;
71 74
72 - NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.tiles.mapbox.com/v3/%@/%f,%f,%i/%ix%i.png", mapID, centerCoordinate.longitude, centerCoordinate.latitude, (int)roundf(initialZoomLevel), (int)roundf(requestFrame.size.width), (int)roundf(requestFrame.size.height)]]; 75 + [self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
73 76
74 - [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imageURL] 77 + [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
75 queue:[NSOperationQueue new] 78 queue:[NSOperationQueue new]
76 completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) 79 completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
77 { 80 {
78 [_spinner removeFromSuperview]; 81 [_spinner removeFromSuperview];
79 [_spinner release]; _spinner = nil; 82 [_spinner release]; _spinner = nil;
80 83
81 - if (responseData) 84 + if ([UIImage imageWithData:responseData])
82 { 85 {
83 dispatch_async(dispatch_get_main_queue(), ^(void) 86 dispatch_async(dispatch_get_main_queue(), ^(void)
84 { 87 {
85 self.image = [UIImage imageWithData:responseData]; 88 self.image = [UIImage imageWithData:responseData];
  89 +
  90 + successBlock(self.image);
86 }); 91 });
87 } 92 }
88 -  
89 else 93 else
90 - return; // TODO: notify delegate of error & display something 94 + {
  95 + dispatch_async(dispatch_get_main_queue(), ^(void)
  96 + {
  97 + failureBlock(error);
  98 + });
  99 + }
91 }]; 100 }];
92 101
93 return self; 102 return self;
94 } 103 }
95 104
  105 +- (void)createMapViewWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
  106 +{
  107 + CGRect requestFrame = CGRectMake(frame.origin.x, frame.origin.y, fminf(frame.size.width, RMStaticMapViewMaxWidth), fminf(frame.size.height, RMStaticMapViewMaxHeight));
  108 +
  109 + if ( ! CLLocationCoordinate2DIsValid(centerCoordinate))
  110 + centerCoordinate = CLLocationCoordinate2DMake(0, 0);
  111 +
  112 + zoomLevel = fmaxf(zoomLevel, RMStaticMapViewMinZoom);
  113 + zoomLevel = fminf(zoomLevel, RMStaticMapViewMaxZoom);
  114 +
  115 + self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];
  116 +
  117 + self.contentMode = UIViewContentModeCenter;
  118 +
  119 + self.showLogoBug = YES;
  120 +
  121 + _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  122 +
  123 + [_spinner startAnimating];
  124 +
  125 + _spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  126 +
  127 + _spinner.center = self.center;
  128 +
  129 + [self addSubview:_spinner];
  130 +
  131 + _requestURL = [[NSURL URLWithString:[NSString stringWithFormat:@"http://api.tiles.mapbox.com/v3/%@/%f,%f,%i/%ix%i.png", mapID, centerCoordinate.longitude, centerCoordinate.latitude, (int)roundf(zoomLevel), (int)roundf(requestFrame.size.width), (int)roundf(requestFrame.size.height)]] retain];
  132 +}
  133 +
96 - (void)dealloc 134 - (void)dealloc
97 { 135 {
98 [_logoBug release]; _logoBug = nil; 136 [_logoBug release]; _logoBug = nil;
99 [_spinner release]; _spinner = nil; 137 [_spinner release]; _spinner = nil;
  138 + [_requestURL release]; _requestURL = nil;
100 [super dealloc]; 139 [super dealloc];
101 } 140 }
102 141