...
|
...
|
@@ -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
|
|
|
|
|
|
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
|
|
|
{
|
|
|
UIImageView *_logoBug;
|
|
|
UIActivityIndicatorView *_spinner;
|
|
|
NSURL *_requestURL;
|
|
|
return [self initWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel completionHandler:nil];
|
|
|
}
|
|
|
|
|
|
@synthesize showLogoBug=_showLogoBug;
|
|
|
|
|
|
- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
|
|
|
- (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];
|
|
|
self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];
|
|
|
|
|
|
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
|
|
|
queue:[NSOperationQueue new]
|
|
|
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
|
|
|
{
|
|
|
[_spinner removeFromSuperview];
|
|
|
[_spinner release]; _spinner = nil;
|
|
|
self.hideAttribution = YES;
|
|
|
|
|
|
if (responseData)
|
|
|
{
|
|
|
dispatch_async(dispatch_get_main_queue(), ^(void)
|
|
|
{
|
|
|
self.image = [UIImage imageWithData:responseData];
|
|
|
});
|
|
|
}
|
|
|
}];
|
|
|
self.showsUserLocation = NO;
|
|
|
|
|
|
return self;
|
|
|
}
|
|
|
self.userInteractionEnabled = NO;
|
|
|
|
|
|
- (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;
|
|
|
__unsafe_unretained RMStaticMapView *weakSelf = self;
|
|
|
|
|
|
[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];
|
|
|
if (weakSelf)
|
|
|
{
|
|
|
UIImage *image = [weakSelf takeSnapshot];
|
|
|
|
|
|
successBlock(self.image);
|
|
|
});
|
|
|
if (image)
|
|
|
handler(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;
|
|
|
|
|
|
_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];
|
|
|
}
|
|
|
|
|
|
- (void)setShowLogoBug:(BOOL)showLogoBug
|
|
|
{
|
|
|
if (showLogoBug && ! _logoBug)
|
|
|
{
|
|
|
_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;
|
|
|
|
|
|
[self addSubview:_logoBug];
|
|
|
}
|
|
|
else if ( ! showLogoBug && _logoBug)
|
|
|
{
|
|
|
[_logoBug removeFromSuperview];
|
|
|
[_logoBug release]; _logoBug = nil;
|
|
|
}
|
|
|
|
|
|
_showLogoBug = showLogoBug;
|
|
|
}
|
|
|
|
|
|
@end |
...
|
...
|
|