Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation language parameter #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SVGeocoder/SVGeocoder.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef void (^SVGeocoderCompletionHandler)(NSArray *placemarks, NSHTTPURLRespon
+ (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;

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

- (SVGeocoder*)initWithAddress:(NSString *)address completion:(SVGeocoderCompletionHandler)block;
- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block;
Expand Down
27 changes: 18 additions & 9 deletions SVGeocoder/SVGeocoder.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ @interface SVGeocoder ()
@property (nonatomic, strong) NSString *requestPath;
@property (nonatomic, strong) NSTimer *timeoutTimer; // see http://stackoverflow.com/questions/2736967

- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block;
- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters language:(NSString *)language completion:(SVGeocoderCompletionHandler)block;

- (void)addParametersToRequest:(NSMutableDictionary*)parameters;
- (void)finish;
Expand Down Expand Up @@ -94,26 +94,35 @@ + (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region components:
}

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

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

#pragma mark - Public Initializers

- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block {
return [self initWithCoordinate:coordinate language:nil completion:block];
}

- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate language:(NSString *)language completion:(SVGeocoderCompletionHandler)block {
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude], @"latlng", nil];

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


- (SVGeocoder*)initWithAddress:(NSString*)address completion:(SVGeocoderCompletionHandler)block {
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
address, @"address", nil];

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


Expand All @@ -124,7 +133,7 @@ - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region com
address, @"address",
bounds, @"bounds", nil];

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

- (SVGeocoder*)initWithAddress:(NSString *)address components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block {
Expand All @@ -134,7 +143,7 @@ - (SVGeocoder*)initWithAddress:(NSString *)address components:(NSDictionary *)co
address, @"address",
componentsValue, @"components", nil];

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

- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block {
Expand All @@ -146,19 +155,19 @@ - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region com
bounds, @"bounds",
componentsValue, @"components", nil];

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

#pragma mark - Private Utility Methods

- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block {
- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters language:(NSString *)language completion:(SVGeocoderCompletionHandler)block {
self = [super init];
self.operationCompletionBlock = block;
self.operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/geocode/json"]];
[self.operationRequest setTimeoutInterval:kSVGeocoderTimeoutInterval];

[parameters setValue:@"true" forKey:@"sensor"];
[parameters setValue:[NSLocale preferredLanguages][0] forKey:@"language"];
[parameters setValue:language ? language : [NSLocale preferredLanguages][0] forKey:@"language"];
[self addParametersToRequest:parameters];

self.state = SVGeocoderStateReady;
Expand Down