Authored by Justin R. Miller

fleshed out success/failure callbacks for static map retrieval

... ... @@ -27,14 +27,36 @@
#import <UIKit/UIKit.h>
/** 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.
*
* @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. */
@interface RMStaticMapView : UIImageView
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)initialZoomLevel;
/** @name Initializing a Static Map View */
/** Initialize a static map view with a given frame, mapID, center coordinate, and zoom level.
* @param frame The frame with which to initialize the map view.
* @param mapID The MapBox map ID string, typically in the format `<username>.map-<random characters>`.
* @param centerCoordinate The map center coordinate.
* @param zoomLevel The map zoom level.
* @return An initialized map view, or `nil` if the map view was unable to be initialized. */
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel;
/** 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.
* @param frame The frame with which to initialize the map view.
* @param mapID The MapBox map ID string, typically in the format `<username>.map-<random characters>`.
* @param centerCoordinate The map center coordinate.
* @param zoomLevel The map zoom level.
* @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.
* @param failureBlock A block to be performed upon map image retrieval failure. The retrieval error is passed as an argument to the block.
* @return An initialized map view, or `nil` if the map view was unable to be initialized. */
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel success:(void (^)(UIImage *))successBlock failure:(void (^)(NSError *))failureBlock;
/** @name Fine-Tuning the Map Appearance */
/** A Boolean value indicating whether to show a small logo in the corner of the map view. Defaults to `YES`. */
@property (nonatomic, assign) BOOL showLogoBug;
// TODO: success/failure blocks
// TODO: markers
// TODO: attribution
... ...
... ... @@ -36,67 +36,106 @@
{
UIImageView *_logoBug;
UIActivityIndicatorView *_spinner;
NSURL *_requestURL;
}
@synthesize showLogoBug=_showLogoBug;
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)initialZoomLevel
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
{
if (!(self = [super initWithFrame:frame]))
return nil;
CGRect requestFrame = CGRectMake(frame.origin.x, frame.origin.y, fminf(frame.size.width, RMStaticMapViewMaxWidth), fminf(frame.size.height, RMStaticMapViewMaxHeight));
if ( ! CLLocationCoordinate2DIsValid(centerCoordinate))
centerCoordinate = CLLocationCoordinate2DMake(0, 0);
initialZoomLevel = fmaxf(initialZoomLevel, RMStaticMapViewMinZoom);
initialZoomLevel = fminf(initialZoomLevel, RMStaticMapViewMaxZoom);
self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];
self.contentMode = UIViewContentModeCenter;
[self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
self.showLogoBug = YES;
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[_spinner startAnimating];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
queue:[NSOperationQueue new]
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
{
[_spinner removeFromSuperview];
[_spinner release]; _spinner = nil;
_spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
if (responseData)
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
self.image = [UIImage imageWithData:responseData];
});
}
}];
_spinner.center = self.center;
return self;
}
[self addSubview:_spinner];
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel success:(void (^)(UIImage *))successBlock failure:(void (^)(NSError *))failureBlock
{
if (!(self = [super initWithFrame:frame]))
return nil;
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)]];
[self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imageURL]
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
queue:[NSOperationQueue new]
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
{
[_spinner removeFromSuperview];
[_spinner release]; _spinner = nil;
if (responseData)
if ([UIImage imageWithData:responseData])
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
self.image = [UIImage imageWithData:responseData];
successBlock(self.image);
});
}
else
return; // TODO: notify delegate of error & display something
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
failureBlock(error);
});
}
}];
return self;
}
- (void)createMapViewWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
{
CGRect requestFrame = CGRectMake(frame.origin.x, frame.origin.y, fminf(frame.size.width, RMStaticMapViewMaxWidth), fminf(frame.size.height, RMStaticMapViewMaxHeight));
if ( ! CLLocationCoordinate2DIsValid(centerCoordinate))
centerCoordinate = CLLocationCoordinate2DMake(0, 0);
zoomLevel = fmaxf(zoomLevel, RMStaticMapViewMinZoom);
zoomLevel = fminf(zoomLevel, RMStaticMapViewMaxZoom);
self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];
self.contentMode = UIViewContentModeCenter;
self.showLogoBug = YES;
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[_spinner startAnimating];
_spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
_spinner.center = self.center;
[self addSubview:_spinner];
_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];
}
- (void)dealloc
{
[_logoBug release]; _logoBug = nil;
[_spinner release]; _spinner = nil;
[_requestURL release]; _requestURL = nil;
[super dealloc];
}
... ...