@@ -43,6 +43,8 @@ - (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(S
43
43
44
44
- (void )addParametersToRequest : (NSMutableDictionary *)parameters ;
45
45
- (void )finish ;
46
+ - (NSString *)createComponentsStringFromDictionary : (NSDictionary *)components ;
47
+ - (NSString *)createBoundsStringFromRegion : (CLRegion *)region ;
46
48
47
49
- (void )connection : (NSURLConnection *)connection didFailWithError : (NSError *)error ;
48
50
- (void )callCompletionBlockWithResponse : (id )response error : (NSError *)error ;
@@ -79,6 +81,18 @@ + (SVGeocoder *)geocode:(NSString *)address region:(CLRegion *)region completion
79
81
return geocoder;
80
82
}
81
83
84
+ + (SVGeocoder*)geocode : (NSString *)address components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
85
+ SVGeocoder *geocoder = [[self alloc ] initWithAddress: address components: components completion: block];
86
+ [geocoder start ];
87
+ return geocoder;
88
+ }
89
+
90
+ + (SVGeocoder*)geocode : (NSString *)address region : (CLRegion *)region components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
91
+ SVGeocoder *geocoder = [[self alloc ] initWithAddress: address region: region components: components completion: block];
92
+ [geocoder start ];
93
+ return geocoder;
94
+ }
95
+
82
96
+ (SVGeocoder *)reverseGeocode : (CLLocationCoordinate2D)coordinate completion : (SVGeocoderCompletionHandler)block {
83
97
SVGeocoder *geocoder = [[self alloc ] initWithCoordinate: coordinate completion: block];
84
98
[geocoder start ];
@@ -104,18 +118,36 @@ - (SVGeocoder*)initWithAddress:(NSString*)address completion:(SVGeocoderCompleti
104
118
105
119
106
120
- (SVGeocoder*)initWithAddress : (NSString *)address region : (CLRegion *)region completion : (SVGeocoderCompletionHandler)block {
107
- MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance (region.center , region.radius , region.radius );
121
+ NSString *bounds = [self createBoundsStringFromRegion: region];
122
+
108
123
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
109
124
address, @" address" ,
110
- [NSString stringWithFormat: @" %f ,%f |%f ,%f " ,
111
- coordinateRegion.center.latitude-(coordinateRegion.span.latitudeDelta/2.0 ),
112
- coordinateRegion.center.longitude-(coordinateRegion.span.longitudeDelta/2.0 ),
113
- coordinateRegion.center.latitude+(coordinateRegion.span.latitudeDelta/2.0 ),
114
- coordinateRegion.center.longitude+(coordinateRegion.span.longitudeDelta/2.0 )], @" bounds" , nil ];
125
+ bounds, @" bounds" , nil ];
115
126
116
127
return [self initWithParameters: parameters completion: block];
117
128
}
118
129
130
+ - (SVGeocoder*)initWithAddress : (NSString *)address components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
131
+ NSString *componentsValue = [self createComponentsStringFromDictionary: components];
132
+
133
+ NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
134
+ address, @" address" ,
135
+ componentsValue, @" components" , nil ];
136
+
137
+ return [self initWithParameters: parameters completion: block];
138
+ }
139
+
140
+ - (SVGeocoder*)initWithAddress : (NSString *)address region : (CLRegion *)region components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
141
+ NSString *bounds = [self createBoundsStringFromRegion: region];
142
+ NSString *componentsValue = [self createComponentsStringFromDictionary: components];
143
+
144
+ NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
145
+ address, @" address" ,
146
+ bounds, @" bounds" ,
147
+ componentsValue, @" components" , nil ];
148
+
149
+ return [self initWithParameters: parameters completion: block];
150
+ }
119
151
120
152
#pragma mark - Private Utility Methods
121
153
@@ -162,10 +194,35 @@ - (void)setTimeoutTimer:(NSTimer *)newTimer {
162
194
timeoutTimer = newTimer;
163
195
}
164
196
197
+ - (NSString *)createComponentsStringFromDictionary : (NSDictionary *)components {
198
+ NSMutableArray *preparedComponents = [NSMutableArray new ];
199
+
200
+ [components enumerateKeysAndObjectsUsingBlock: ^(NSString * key, NSString * value, BOOL *stop) {
201
+ NSString *component = [NSString stringWithFormat: @" %@ :%@ " , key, value];
202
+ [preparedComponents addObject: component];
203
+ }];
204
+
205
+ NSString *componentsValue = [preparedComponents componentsJoinedByString: @" |" ];
206
+
207
+ return componentsValue;
208
+ }
209
+
210
+ - (NSString *)createBoundsStringFromRegion : (CLRegion *)region {
211
+ MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance (region.center , region.radius , region.radius );
212
+
213
+ NSString *bounds = [NSString stringWithFormat: @" %f ,%f |%f ,%f " ,
214
+ coordinateRegion.center.latitude-(coordinateRegion.span.latitudeDelta/2.0 ),
215
+ coordinateRegion.center.longitude-(coordinateRegion.span.longitudeDelta/2.0 ),
216
+ coordinateRegion.center.latitude+(coordinateRegion.span.latitudeDelta/2.0 ),
217
+ coordinateRegion.center.longitude+(coordinateRegion.span.longitudeDelta/2.0 )];
218
+
219
+ return bounds;
220
+ }
221
+
165
222
#pragma mark - NSOperation methods
166
223
167
224
- (void )start {
168
-
225
+
169
226
if (self.isCancelled ) {
170
227
[self finish ];
171
228
return ;
0 commit comments