@@ -44,6 +44,8 @@ - (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(S
44
44
- (void )addParametersToRequest : (NSMutableDictionary *)parameters ;
45
45
46
46
- (void )finish ;
47
+ - (NSString *)createComponentsStringFromDictionary : (NSDictionary *)components ;
48
+ - (NSString *)createBoundsStringFromRegion : (CLRegion *)region ;
47
49
48
50
- (void )connection : (NSURLConnection *)connection didFailWithError : (NSError *)error ;
49
51
@@ -81,12 +83,30 @@ + (SVGeocoder *)geocode:(NSString *)address region:(CLRegion *)region completion
81
83
return geocoder;
82
84
}
83
85
86
+ + (SVGeocoder*)geocode : (NSString *)address components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
87
+ SVGeocoder *geocoder = [[self alloc ] initWithAddress: address components: components completion: block];
88
+ [geocoder start ];
89
+ return geocoder;
90
+ }
91
+
92
+ + (SVGeocoder*)geocode : (NSString *)address region : (CLRegion *)region components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
93
+ SVGeocoder *geocoder = [[self alloc ] initWithAddress: address region: region components: components completion: block];
94
+ [geocoder start ];
95
+ return geocoder;
96
+ }
97
+
84
98
+ (SVGeocoder *)reverseGeocode : (CLLocationCoordinate2D)coordinate completion : (SVGeocoderCompletionHandler)block {
85
99
SVGeocoder *geocoder = [[self alloc ] initWithCoordinate: coordinate completion: block];
86
100
[geocoder start ];
87
101
return geocoder;
88
102
}
89
103
104
+ + (void )setGoogleMapsAPIKey : (NSString *)key {
105
+
106
+ googleMapsAPIKey = [key copy ];
107
+
108
+ }
109
+
90
110
#pragma mark - Public Initializers
91
111
92
112
- (SVGeocoder*)initWithCoordinate : (CLLocationCoordinate2D)coordinate completion : (SVGeocoderCompletionHandler)block {
@@ -105,22 +125,35 @@ - (SVGeocoder*)initWithAddress:(NSString*)address completion:(SVGeocoderCompleti
105
125
}
106
126
107
127
- (SVGeocoder*)initWithAddress : (NSString *)address region : (CLRegion *)region completion : (SVGeocoderCompletionHandler)block {
108
- MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance (region.center , region.radius , region.radius );
128
+ NSString *bounds = [self createBoundsStringFromRegion: region];
129
+
109
130
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
110
131
address, @" address" ,
111
- [NSString stringWithFormat: @" %f ,%f |%f ,%f " ,
112
- coordinateRegion.center.latitude-(coordinateRegion.span.latitudeDelta/2.0 ),
113
- coordinateRegion.center.longitude-(coordinateRegion.span.longitudeDelta/2.0 ),
114
- coordinateRegion.center.latitude+(coordinateRegion.span.latitudeDelta/2.0 ),
115
- coordinateRegion.center.longitude+(coordinateRegion.span.longitudeDelta/2.0 )], @" bounds" , nil ];
132
+ bounds, @" bounds" , nil ];
116
133
117
134
return [self initWithParameters: parameters completion: block];
118
135
}
119
136
120
- + (void )setGoogleMapsAPIKey : (NSString *)key {
137
+ - (SVGeocoder*)initWithAddress : (NSString *)address components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
138
+ NSString *componentsValue = [self createComponentsStringFromDictionary: components];
139
+
140
+ NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
141
+ address, @" address" ,
142
+ componentsValue, @" components" , nil ];
143
+
144
+ return [self initWithParameters: parameters completion: block];
145
+ }
121
146
122
- googleMapsAPIKey = [key copy ];
147
+ - (SVGeocoder*)initWithAddress : (NSString *)address region : (CLRegion *)region components : (NSDictionary *)components completion : (SVGeocoderCompletionHandler)block {
148
+ NSString *bounds = [self createBoundsStringFromRegion: region];
149
+ NSString *componentsValue = [self createComponentsStringFromDictionary: components];
150
+
151
+ NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
152
+ address, @" address" ,
153
+ bounds, @" bounds" ,
154
+ componentsValue, @" components" , nil ];
123
155
156
+ return [self initWithParameters: parameters completion: block];
124
157
}
125
158
126
159
#pragma mark - Private Utility Methods
@@ -173,10 +206,35 @@ - (void)setTimeoutTimer:(NSTimer *)newTimer {
173
206
timeoutTimer = newTimer;
174
207
}
175
208
209
+ - (NSString *)createComponentsStringFromDictionary : (NSDictionary *)components {
210
+ NSMutableArray *preparedComponents = [NSMutableArray new ];
211
+
212
+ [components enumerateKeysAndObjectsUsingBlock: ^(NSString * key, NSString * value, BOOL *stop) {
213
+ NSString *component = [NSString stringWithFormat: @" %@ :%@ " , key, value];
214
+ [preparedComponents addObject: component];
215
+ }];
216
+
217
+ NSString *componentsValue = [preparedComponents componentsJoinedByString: @" |" ];
218
+
219
+ return componentsValue;
220
+ }
221
+
222
+ - (NSString *)createBoundsStringFromRegion : (CLRegion *)region {
223
+ MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance (region.center , region.radius , region.radius );
224
+
225
+ NSString *bounds = [NSString stringWithFormat: @" %f ,%f |%f ,%f " ,
226
+ coordinateRegion.center.latitude-(coordinateRegion.span.latitudeDelta/2.0 ),
227
+ coordinateRegion.center.longitude-(coordinateRegion.span.longitudeDelta/2.0 ),
228
+ coordinateRegion.center.latitude+(coordinateRegion.span.latitudeDelta/2.0 ),
229
+ coordinateRegion.center.longitude+(coordinateRegion.span.longitudeDelta/2.0 )];
230
+
231
+ return bounds;
232
+ }
233
+
176
234
#pragma mark - NSOperation methods
177
235
178
236
- (void )start {
179
-
237
+
180
238
if (self.isCancelled ) {
181
239
[self finish ];
182
240
return ;
0 commit comments