Authored by Justin R. Miller

refs #64: replaced server-based static map with client-side subclass

... ... @@ -27,10 +27,12 @@
#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.
/** 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 a completion handler that can be performed upon map load.
*
* Note that although RMStaticMapView is a subclass of RMMapView, it is provided for convenience only. Unpredictable results may occur if the view is treated as a normal, dynamic map view. If you need to support annotations, multiple tile sources, or other complex functionality, you should use RMMapView directly.
*
* @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. You are also responsible for displaying attribution details for the data elsewhere in your application, if applicable. */
@interface RMStaticMapView : UIImageView
@interface RMStaticMapView : RMMapView
/** @name Initializing a Static Map View */
... ... @@ -42,21 +44,15 @@
* @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.
/** Initialize a static map view with a given frame, mapID, center coordinate, and zoom level, performing a block upon completion of the map load.
* @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.
* @param handler A block to be performed upon map load completion. An image of the map is passed as an argument to the block in the event that you wish to use it elsewhere. The handler will be called on the main dispatch queue.
* @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;
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel completionHandler:(void (^)(UIImage *))handler;
// TODO: markers
// TODO: static markers
@end
... ...
... ... @@ -27,136 +27,41 @@
#import "RMStaticMapView.h"
#define RMStaticMapViewMaxWidth 640.0f
#define RMStaticMapViewMaxHeight 640.0f
#define RMStaticMapViewMinZoom 0.0f
#define RMStaticMapViewMaxZoom 17.0f
@implementation RMStaticMapView
{
UIImageView *_logoBug;
UIActivityIndicatorView *_spinner;
NSURL *_requestURL;
}
@synthesize showLogoBug=_showLogoBug;
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
{
if (!(self = [super initWithFrame:frame]))
return nil;
[self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
queue:[NSOperationQueue new]
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
{
[_spinner removeFromSuperview];
[_spinner release]; _spinner = nil;
if (responseData)
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
self.image = [UIImage imageWithData:responseData];
});
}
}];
return self;
return [self initWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel completionHandler:nil];
}
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel success:(void (^)(UIImage *))successBlock failure:(void (^)(NSError *))failureBlock
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel completionHandler:(void (^)(UIImage *))handler
{
if (!(self = [super initWithFrame:frame]))
if (!(self = [super initWithFrame:frame andTilesource:[[[RMMapBoxSource alloc] initWithMapID:mapID] autorelease] centerCoordinate:centerCoordinate zoomLevel:zoomLevel maxZoomLevel:zoomLevel minZoomLevel:zoomLevel backgroundImage:nil]))
return nil;
[self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
queue:[NSOperationQueue new]
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
{
[_spinner removeFromSuperview];
[_spinner release]; _spinner = nil;
if ([UIImage imageWithData:responseData])
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
self.image = [UIImage imageWithData:responseData];
successBlock(self.image);
});
}
else
{
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;
self.hideAttribution = YES;
_spinner.center = self.center;
self.showsUserLocation = NO;
[self addSubview:_spinner];
self.userInteractionEnabled = NO;
_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];
}
__unsafe_unretained RMStaticMapView *weakSelf = self;
- (void)setShowLogoBug:(BOOL)showLogoBug
{
if (showLogoBug && ! _logoBug)
dispatch_async(dispatch_get_main_queue(), ^(void)
{
_logoBug = [[UIImageView alloc] initWithImage:[RMMapView resourceImageNamed:@"mapbox.png"]];
_logoBug.frame = CGRectMake(8, self.bounds.size.height - _logoBug.bounds.size.height - 4, _logoBug.bounds.size.width, _logoBug.bounds.size.height);
_logoBug.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
if (weakSelf)
{
UIImage *image = [weakSelf takeSnapshot];
[self addSubview:_logoBug];
}
else if ( ! showLogoBug && _logoBug)
{
[_logoBug removeFromSuperview];
[_logoBug release]; _logoBug = nil;
}
if (image)
handler(image);
}
});
_showLogoBug = showLogoBug;
return self;
}
@end
... ...