Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
Conflicts:
	SVGeocoder/SVGeocoder.m
  • Loading branch information
jturolla committed Mar 28, 2014
2 parents 3f5dd83 + 780dc53 commit 291c7e7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
11 changes: 11 additions & 0 deletions SVGeocoder/SVGeocoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

#import "SVPlacemark.h"

#define SVGeocoderComponentRoute @"route"
#define SVGeocoderComponentLocality @"locality"
#define SVGeocoderAdministrativeArea @"administrative_area"
#define SVGeocoderPostalCode @"postal_code"
#define SVGeocoderCountry @"country"

typedef enum {
SVGeocoderZeroResultsError = 1,
SVGeocoderOverQueryLimitError,
Expand All @@ -34,12 +40,17 @@ typedef void (^SVGeocoderCompletionHandler)(NSArray *placemarks, NSHTTPURLRespon
+ (SVGeocoder*)geocode:(NSString *)address completion:(SVGeocoderCompletionHandler)block;

+ (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block;
+ (SVGeocoder*)geocode:(NSString *)address components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;
+ (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;

+ (SVGeocoder*)reverseGeocode:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block;

- (SVGeocoder*)initWithAddress:(NSString *)address completion:(SVGeocoderCompletionHandler)block;

- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block;
- (SVGeocoder*)initWithAddress:(NSString *)address components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;
- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;


- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block;

Expand Down
76 changes: 67 additions & 9 deletions SVGeocoder/SVGeocoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ - (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(S
- (void)addParametersToRequest:(NSMutableDictionary*)parameters;

- (void)finish;
- (NSString*)createComponentsStringFromDictionary:(NSDictionary *)components;
- (NSString*)createBoundsStringFromRegion:(CLRegion *)region;

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

Expand Down Expand Up @@ -81,12 +83,30 @@ + (SVGeocoder *)geocode:(NSString *)address region:(CLRegion *)region completion
return geocoder;
}

+ (SVGeocoder*)geocode:(NSString *)address components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block {
SVGeocoder *geocoder = [[self alloc] initWithAddress:address components:components completion:block];
[geocoder start];
return geocoder;
}

+ (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block {
SVGeocoder *geocoder = [[self alloc] initWithAddress:address region:region components:components completion:block];
[geocoder start];
return geocoder;
}

+ (SVGeocoder *)reverseGeocode:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block {
SVGeocoder *geocoder = [[self alloc] initWithCoordinate:coordinate completion:block];
[geocoder start];
return geocoder;
}

+ (void)setGoogleMapsAPIKey:(NSString *)key {

googleMapsAPIKey = [key copy];

}

#pragma mark - Public Initializers

- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block {
Expand All @@ -105,22 +125,35 @@ - (SVGeocoder*)initWithAddress:(NSString*)address completion:(SVGeocoderCompleti
}

- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block {
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance(region.center, region.radius, region.radius);
NSString *bounds = [self createBoundsStringFromRegion:region];

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
address, @"address",
[NSString stringWithFormat:@"%f,%f|%f,%f",
coordinateRegion.center.latitude-(coordinateRegion.span.latitudeDelta/2.0),
coordinateRegion.center.longitude-(coordinateRegion.span.longitudeDelta/2.0),
coordinateRegion.center.latitude+(coordinateRegion.span.latitudeDelta/2.0),
coordinateRegion.center.longitude+(coordinateRegion.span.longitudeDelta/2.0)], @"bounds", nil];
bounds, @"bounds", nil];

return [self initWithParameters:parameters completion:block];
}

+ (void)setGoogleMapsAPIKey:(NSString *)key {
- (SVGeocoder*)initWithAddress:(NSString *)address components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block {
NSString *componentsValue = [self createComponentsStringFromDictionary:components];

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
address, @"address",
componentsValue, @"components", nil];

return [self initWithParameters:parameters completion:block];
}

googleMapsAPIKey = [key copy];
- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block {
NSString *bounds = [self createBoundsStringFromRegion:region];
NSString *componentsValue = [self createComponentsStringFromDictionary:components];

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
address, @"address",
bounds, @"bounds",
componentsValue, @"components", nil];

return [self initWithParameters:parameters completion:block];
}

#pragma mark - Private Utility Methods
Expand Down Expand Up @@ -173,10 +206,35 @@ - (void)setTimeoutTimer:(NSTimer *)newTimer {
timeoutTimer = newTimer;
}

- (NSString*)createComponentsStringFromDictionary:(NSDictionary *)components {
NSMutableArray *preparedComponents = [NSMutableArray new];

[components enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSString* value, BOOL *stop) {
NSString *component = [NSString stringWithFormat:@"%@:%@", key, value];
[preparedComponents addObject:component];
}];

NSString *componentsValue = [preparedComponents componentsJoinedByString:@"|"];

return componentsValue;
}

- (NSString*)createBoundsStringFromRegion:(CLRegion *)region {
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance(region.center, region.radius, region.radius);

NSString *bounds = [NSString stringWithFormat:@"%f,%f|%f,%f",
coordinateRegion.center.latitude-(coordinateRegion.span.latitudeDelta/2.0),
coordinateRegion.center.longitude-(coordinateRegion.span.longitudeDelta/2.0),
coordinateRegion.center.latitude+(coordinateRegion.span.latitudeDelta/2.0),
coordinateRegion.center.longitude+(coordinateRegion.span.longitudeDelta/2.0)];

return bounds;
}

#pragma mark - NSOperation methods

- (void)start {

if(self.isCancelled) {
[self finish];
return;
Expand Down

0 comments on commit 291c7e7

Please sign in to comment.