|
@@ -36,67 +36,106 @@ |
|
@@ -36,67 +36,106 @@ |
36
|
{
|
36
|
{
|
37
|
UIImageView *_logoBug;
|
37
|
UIImageView *_logoBug;
|
38
|
UIActivityIndicatorView *_spinner;
|
38
|
UIActivityIndicatorView *_spinner;
|
|
|
39
|
+ NSURL *_requestURL;
|
39
|
}
|
40
|
}
|
40
|
|
41
|
|
41
|
@synthesize showLogoBug=_showLogoBug;
|
42
|
@synthesize showLogoBug=_showLogoBug;
|
42
|
|
43
|
|
43
|
-- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)initialZoomLevel
|
44
|
+- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
|
44
|
{
|
45
|
{
|
45
|
if (!(self = [super initWithFrame:frame]))
|
46
|
if (!(self = [super initWithFrame:frame]))
|
46
|
return nil;
|
47
|
return nil;
|
47
|
|
48
|
|
48
|
- CGRect requestFrame = CGRectMake(frame.origin.x, frame.origin.y, fminf(frame.size.width, RMStaticMapViewMaxWidth), fminf(frame.size.height, RMStaticMapViewMaxHeight));
|
|
|
49
|
-
|
|
|
50
|
- if ( ! CLLocationCoordinate2DIsValid(centerCoordinate))
|
|
|
51
|
- centerCoordinate = CLLocationCoordinate2DMake(0, 0);
|
|
|
52
|
-
|
|
|
53
|
- initialZoomLevel = fmaxf(initialZoomLevel, RMStaticMapViewMinZoom);
|
|
|
54
|
- initialZoomLevel = fminf(initialZoomLevel, RMStaticMapViewMaxZoom);
|
|
|
55
|
-
|
|
|
56
|
- self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];
|
|
|
57
|
-
|
|
|
58
|
- self.contentMode = UIViewContentModeCenter;
|
49
|
+ [self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
|
59
|
|
50
|
|
60
|
- self.showLogoBug = YES;
|
|
|
61
|
-
|
|
|
62
|
- _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
|
|
|
63
|
-
|
|
|
64
|
- [_spinner startAnimating];
|
51
|
+ [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
|
|
|
52
|
+ queue:[NSOperationQueue new]
|
|
|
53
|
+ completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
|
|
|
54
|
+ {
|
|
|
55
|
+ [_spinner removeFromSuperview];
|
|
|
56
|
+ [_spinner release]; _spinner = nil;
|
65
|
|
57
|
|
66
|
- _spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
|
58
|
+ if (responseData)
|
|
|
59
|
+ {
|
|
|
60
|
+ dispatch_async(dispatch_get_main_queue(), ^(void)
|
|
|
61
|
+ {
|
|
|
62
|
+ self.image = [UIImage imageWithData:responseData];
|
|
|
63
|
+ });
|
|
|
64
|
+ }
|
|
|
65
|
+ }];
|
67
|
|
66
|
|
68
|
- _spinner.center = self.center;
|
67
|
+ return self;
|
|
|
68
|
+}
|
69
|
|
69
|
|
70
|
- [self addSubview:_spinner];
|
70
|
+- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel success:(void (^)(UIImage *))successBlock failure:(void (^)(NSError *))failureBlock
|
|
|
71
|
+{
|
|
|
72
|
+ if (!(self = [super initWithFrame:frame]))
|
|
|
73
|
+ return nil;
|
71
|
|
74
|
|
72
|
- 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)]];
|
75
|
+ [self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
|
73
|
|
76
|
|
74
|
- [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imageURL]
|
77
|
+ [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
|
75
|
queue:[NSOperationQueue new]
|
78
|
queue:[NSOperationQueue new]
|
76
|
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
|
79
|
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
|
77
|
{
|
80
|
{
|
78
|
[_spinner removeFromSuperview];
|
81
|
[_spinner removeFromSuperview];
|
79
|
[_spinner release]; _spinner = nil;
|
82
|
[_spinner release]; _spinner = nil;
|
80
|
|
83
|
|
81
|
- if (responseData)
|
84
|
+ if ([UIImage imageWithData:responseData])
|
82
|
{
|
85
|
{
|
83
|
dispatch_async(dispatch_get_main_queue(), ^(void)
|
86
|
dispatch_async(dispatch_get_main_queue(), ^(void)
|
84
|
{
|
87
|
{
|
85
|
self.image = [UIImage imageWithData:responseData];
|
88
|
self.image = [UIImage imageWithData:responseData];
|
|
|
89
|
+
|
|
|
90
|
+ successBlock(self.image);
|
86
|
});
|
91
|
});
|
87
|
}
|
92
|
}
|
88
|
-
|
|
|
89
|
else
|
93
|
else
|
90
|
- return; // TODO: notify delegate of error & display something
|
94
|
+ {
|
|
|
95
|
+ dispatch_async(dispatch_get_main_queue(), ^(void)
|
|
|
96
|
+ {
|
|
|
97
|
+ failureBlock(error);
|
|
|
98
|
+ });
|
|
|
99
|
+ }
|
91
|
}];
|
100
|
}];
|
92
|
|
101
|
|
93
|
return self;
|
102
|
return self;
|
94
|
}
|
103
|
}
|
95
|
|
104
|
|
|
|
105
|
+- (void)createMapViewWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
|
|
|
106
|
+{
|
|
|
107
|
+ CGRect requestFrame = CGRectMake(frame.origin.x, frame.origin.y, fminf(frame.size.width, RMStaticMapViewMaxWidth), fminf(frame.size.height, RMStaticMapViewMaxHeight));
|
|
|
108
|
+
|
|
|
109
|
+ if ( ! CLLocationCoordinate2DIsValid(centerCoordinate))
|
|
|
110
|
+ centerCoordinate = CLLocationCoordinate2DMake(0, 0);
|
|
|
111
|
+
|
|
|
112
|
+ zoomLevel = fmaxf(zoomLevel, RMStaticMapViewMinZoom);
|
|
|
113
|
+ zoomLevel = fminf(zoomLevel, RMStaticMapViewMaxZoom);
|
|
|
114
|
+
|
|
|
115
|
+ self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];
|
|
|
116
|
+
|
|
|
117
|
+ self.contentMode = UIViewContentModeCenter;
|
|
|
118
|
+
|
|
|
119
|
+ self.showLogoBug = YES;
|
|
|
120
|
+
|
|
|
121
|
+ _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
|
|
|
122
|
+
|
|
|
123
|
+ [_spinner startAnimating];
|
|
|
124
|
+
|
|
|
125
|
+ _spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
|
|
|
126
|
+
|
|
|
127
|
+ _spinner.center = self.center;
|
|
|
128
|
+
|
|
|
129
|
+ [self addSubview:_spinner];
|
|
|
130
|
+
|
|
|
131
|
+ _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];
|
|
|
132
|
+}
|
|
|
133
|
+
|
96
|
- (void)dealloc
|
134
|
- (void)dealloc
|
97
|
{
|
135
|
{
|
98
|
[_logoBug release]; _logoBug = nil;
|
136
|
[_logoBug release]; _logoBug = nil;
|
99
|
[_spinner release]; _spinner = nil;
|
137
|
[_spinner release]; _spinner = nil;
|
|
|
138
|
+ [_requestURL release]; _requestURL = nil;
|
100
|
[super dealloc];
|
139
|
[super dealloc];
|
101
|
}
|
140
|
}
|
102
|
|
141
|
|