...
|
...
|
@@ -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];
|
|
|
}
|
|
|
|
...
|
...
|
|