|
@@ -27,136 +27,41 @@ |
|
@@ -27,136 +27,41 @@ |
27
|
|
27
|
|
28
|
#import "RMStaticMapView.h"
|
28
|
#import "RMStaticMapView.h"
|
29
|
|
29
|
|
30
|
-#define RMStaticMapViewMaxWidth 640.0f
|
|
|
31
|
-#define RMStaticMapViewMaxHeight 640.0f
|
|
|
32
|
-#define RMStaticMapViewMinZoom 0.0f
|
|
|
33
|
-#define RMStaticMapViewMaxZoom 17.0f
|
|
|
34
|
-
|
|
|
35
|
@implementation RMStaticMapView
|
30
|
@implementation RMStaticMapView
|
|
|
31
|
+
|
|
|
32
|
+- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
|
36
|
{
|
33
|
{
|
37
|
- UIImageView *_logoBug;
|
|
|
38
|
- UIActivityIndicatorView *_spinner;
|
|
|
39
|
- NSURL *_requestURL;
|
34
|
+ return [self initWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel completionHandler:nil];
|
40
|
}
|
35
|
}
|
41
|
|
36
|
|
42
|
-@synthesize showLogoBug=_showLogoBug;
|
|
|
43
|
-
|
|
|
44
|
-- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel
|
37
|
+- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel completionHandler:(void (^)(UIImage *))handler
|
45
|
{
|
38
|
{
|
46
|
- if (!(self = [super initWithFrame:frame]))
|
39
|
+ if (!(self = [super initWithFrame:frame andTilesource:[[[RMMapBoxSource alloc] initWithMapID:mapID] autorelease] centerCoordinate:centerCoordinate zoomLevel:zoomLevel maxZoomLevel:zoomLevel minZoomLevel:zoomLevel backgroundImage:nil]))
|
47
|
return nil;
|
40
|
return nil;
|
48
|
|
41
|
|
49
|
- [self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
|
42
|
+ self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]];
|
50
|
|
43
|
|
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;
|
44
|
+ self.hideAttribution = YES;
|
57
|
|
45
|
|
58
|
- if (responseData)
|
|
|
59
|
- {
|
|
|
60
|
- dispatch_async(dispatch_get_main_queue(), ^(void)
|
|
|
61
|
- {
|
|
|
62
|
- self.image = [UIImage imageWithData:responseData];
|
|
|
63
|
- });
|
|
|
64
|
- }
|
|
|
65
|
- }];
|
46
|
+ self.showsUserLocation = NO;
|
66
|
|
47
|
|
67
|
- return self;
|
|
|
68
|
-}
|
48
|
+ self.userInteractionEnabled = NO;
|
69
|
|
49
|
|
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;
|
50
|
+ __unsafe_unretained RMStaticMapView *weakSelf = self;
|
74
|
|
51
|
|
75
|
- [self createMapViewWithFrame:frame mapID:mapID centerCoordinate:centerCoordinate zoomLevel:zoomLevel];
|
|
|
76
|
-
|
|
|
77
|
- [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[_requestURL autorelease]]
|
|
|
78
|
- queue:[NSOperationQueue new]
|
|
|
79
|
- completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error)
|
|
|
80
|
- {
|
|
|
81
|
- [_spinner removeFromSuperview];
|
|
|
82
|
- [_spinner release]; _spinner = nil;
|
|
|
83
|
-
|
|
|
84
|
- if ([UIImage imageWithData:responseData])
|
|
|
85
|
- {
|
|
|
86
|
dispatch_async(dispatch_get_main_queue(), ^(void)
|
52
|
dispatch_async(dispatch_get_main_queue(), ^(void)
|
87
|
{
|
53
|
{
|
88
|
- self.image = [UIImage imageWithData:responseData];
|
54
|
+ if (weakSelf)
|
|
|
55
|
+ {
|
|
|
56
|
+ UIImage *image = [weakSelf takeSnapshot];
|
89
|
|
57
|
|
90
|
- successBlock(self.image);
|
|
|
91
|
- });
|
58
|
+ if (image)
|
|
|
59
|
+ handler(image);
|
92
|
}
|
60
|
}
|
93
|
- else
|
|
|
94
|
- {
|
|
|
95
|
- dispatch_async(dispatch_get_main_queue(), ^(void)
|
|
|
96
|
- {
|
|
|
97
|
- failureBlock(error);
|
|
|
98
|
});
|
61
|
});
|
99
|
- }
|
|
|
100
|
- }];
|
|
|
101
|
|
62
|
|
102
|
return self;
|
63
|
return self;
|
103
|
-}
|
|
|
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
|
-
|
|
|
134
|
-- (void)dealloc
|
|
|
135
|
-{
|
|
|
136
|
- [_logoBug release]; _logoBug = nil;
|
|
|
137
|
- [_spinner release]; _spinner = nil;
|
|
|
138
|
- [_requestURL release]; _requestURL = nil;
|
|
|
139
|
- [super dealloc];
|
|
|
140
|
-}
|
|
|
141
|
-
|
|
|
142
|
-- (void)setShowLogoBug:(BOOL)showLogoBug
|
|
|
143
|
-{
|
|
|
144
|
- if (showLogoBug && ! _logoBug)
|
|
|
145
|
- {
|
|
|
146
|
- _logoBug = [[UIImageView alloc] initWithImage:[RMMapView resourceImageNamed:@"mapbox.png"]];
|
|
|
147
|
-
|
|
|
148
|
- _logoBug.frame = CGRectMake(8, self.bounds.size.height - _logoBug.bounds.size.height - 4, _logoBug.bounds.size.width, _logoBug.bounds.size.height);
|
|
|
149
|
- _logoBug.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
|
|
|
150
|
-
|
|
|
151
|
- [self addSubview:_logoBug];
|
|
|
152
|
- }
|
|
|
153
|
- else if ( ! showLogoBug && _logoBug)
|
|
|
154
|
- {
|
|
|
155
|
- [_logoBug removeFromSuperview];
|
|
|
156
|
- [_logoBug release]; _logoBug = nil;
|
|
|
157
|
- }
|
|
|
158
|
|
64
|
|
159
|
- _showLogoBug = showLogoBug;
|
|
|
160
|
}
|
65
|
}
|
161
|
|
66
|
|
162
|
@end |
67
|
@end |