...
|
...
|
@@ -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 |
...
|
...
|
|