From 8119549dc4e8eb2e693009543cc389c0900cd74e Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Tue, 25 Mar 2014 16:10:31 -0300 Subject: [PATCH 1/8] Added google api token as a parameter for the request. --- SVGeocoder/SVGeocoder.h | 1 + SVGeocoder/SVGeocoder.m | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/SVGeocoder/SVGeocoder.h b/SVGeocoder/SVGeocoder.h index 74995a0..c61af81 100644 --- a/SVGeocoder/SVGeocoder.h +++ b/SVGeocoder/SVGeocoder.h @@ -36,6 +36,7 @@ typedef void (^SVGeocoderCompletionHandler)(NSArray *placemarks, NSHTTPURLRespon - (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block; +- (void)setGoogleMapsToken:(NSString*)token; - (void)start; - (void)cancel; diff --git a/SVGeocoder/SVGeocoder.m b/SVGeocoder/SVGeocoder.m index 63861ad..96a8a28 100644 --- a/SVGeocoder/SVGeocoder.m +++ b/SVGeocoder/SVGeocoder.m @@ -116,6 +116,11 @@ - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region com return [self initWithParameters:parameters completion:block]; } +- (void)setGoogleMapsToken:(NSString *)token { + + [self addParametersToRequest:[@{@"token": token} mutableCopy]]; + +} #pragma mark - Private Utility Methods From 3b4eb91ac9fd3dcab2452a668b05459b481d2c8a Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Tue, 25 Mar 2014 16:14:20 -0300 Subject: [PATCH 2/8] Updating podspec and readme. --- README.textile | 7 +++++++ SVGeocoder.podspec | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.textile b/README.textile index 11ea0ef..d7f11b7 100644 --- a/README.textile +++ b/README.textile @@ -62,6 +62,13 @@ SVPlacemark is now a simple NSObject. Here's a sample placemark returned for str There's also a @region@ (@MKCoordinateRegion@) property in case the returned placemark isn't a pinned location, and a @location@ (@CLLocation@) convenience property. +h2. Google Maps API Token + +You can include a Google Maps API token by initializing the geocoder and setting it. + +
+    [geocoder setGoogleMapsToken:@"token"];
+
h2. Credits diff --git a/SVGeocoder.podspec b/SVGeocoder.podspec index f8af951..9588016 100644 --- a/SVGeocoder.podspec +++ b/SVGeocoder.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SVGeocoder' - s.version = '0.1' + s.version = '0.2' s.license = 'MIT' s.platform = :ios s.summary = 'Simple Cocoa wrapper for the Google Geocoding Service.' From ffb8c2019ae9e92052dff646d63a88ef2dfd0039 Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Tue, 25 Mar 2014 16:31:26 -0300 Subject: [PATCH 3/8] Renaming token to key on the parameters. --- README.textile | 4 ++-- SVGeocoder/SVGeocoder.m | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.textile b/README.textile index d7f11b7..14af39b 100644 --- a/README.textile +++ b/README.textile @@ -64,10 +64,10 @@ There's also a @region@ (@MKCoordinateRegion@) property in case the returned pla h2. Google Maps API Token -You can include a Google Maps API token by initializing the geocoder and setting it. +You can include a Google Maps API token by initializing the geocoder and setting it. You should use the default initializers to include the token.
-    [geocoder setGoogleMapsToken:@"token"];
+[geocoder setGoogleMapsToken:@"token"];
 
h2. Credits diff --git a/SVGeocoder/SVGeocoder.m b/SVGeocoder/SVGeocoder.m index 96a8a28..1bdd211 100644 --- a/SVGeocoder/SVGeocoder.m +++ b/SVGeocoder/SVGeocoder.m @@ -118,7 +118,7 @@ - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region com - (void)setGoogleMapsToken:(NSString *)token { - [self addParametersToRequest:[@{@"token": token} mutableCopy]]; + [self addParametersToRequest:[@{@"key": token} mutableCopy]]; } From 118a220cb57c37642298bf5f0de23ec1ab314a71 Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Thu, 27 Mar 2014 17:25:59 -0300 Subject: [PATCH 4/8] Renamed token to key, creating static var to set the key for all requests. --- Demo/Classes/SVGeocoderAppAppDelegate.m | 2 ++ SVGeocoder/SVGeocoder.h | 9 ++++++++- SVGeocoder/SVGeocoder.m | 14 ++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Demo/Classes/SVGeocoderAppAppDelegate.m b/Demo/Classes/SVGeocoderAppAppDelegate.m index 2fb7d98..e469e4a 100644 --- a/Demo/Classes/SVGeocoderAppAppDelegate.m +++ b/Demo/Classes/SVGeocoderAppAppDelegate.m @@ -25,6 +25,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( // Add the view controller's view to the window and display. [self.window addSubview:viewController.view]; [self.window makeKeyAndVisible]; + + [SVGeocoder setGoogleMapsAPIKey:@"KEY"]; return YES; } diff --git a/SVGeocoder/SVGeocoder.h b/SVGeocoder/SVGeocoder.h index c61af81..f3c74dc 100644 --- a/SVGeocoder/SVGeocoder.h +++ b/SVGeocoder/SVGeocoder.h @@ -26,18 +26,25 @@ typedef void (^SVGeocoderCompletionHandler)(NSArray *placemarks, NSHTTPURLRespon @interface SVGeocoder : NSOperation +// Set static Google Maps API Key for all requests. +// The key will be included in the request if it's +// not nil. ++ (void)setGoogleMapsAPIKey:(NSString*)key; + + (SVGeocoder*)geocode:(NSString *)address completion:(SVGeocoderCompletionHandler)block; + + (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region 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*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block; -- (void)setGoogleMapsToken:(NSString*)token; - (void)start; + - (void)cancel; @end \ No newline at end of file diff --git a/SVGeocoder/SVGeocoder.m b/SVGeocoder/SVGeocoder.m index 1bdd211..d6e0550 100644 --- a/SVGeocoder/SVGeocoder.m +++ b/SVGeocoder/SVGeocoder.m @@ -20,7 +20,7 @@ }; typedef NSUInteger SVGeocoderState; - +static NSString *googleMapsAPIKey; @interface NSString (URLEncoding) - (NSString*)encodedURLParameterString; @@ -42,9 +42,11 @@ @interface SVGeocoder () - (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block; - (void)addParametersToRequest:(NSMutableDictionary*)parameters; + - (void)finish; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; + - (void)callCompletionBlockWithResponse:(id)response error:(NSError *)error; @end @@ -102,7 +104,6 @@ - (SVGeocoder*)initWithAddress:(NSString*)address completion:(SVGeocoderCompleti return [self initWithParameters:parameters completion:block]; } - - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block { MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance(region.center, region.radius, region.radius); NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: @@ -116,9 +117,9 @@ - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region com return [self initWithParameters:parameters completion:block]; } -- (void)setGoogleMapsToken:(NSString *)token { ++ (void)setGoogleMapsAPIKey:(NSString *)key { - [self addParametersToRequest:[@{@"key": token} mutableCopy]]; + googleMapsAPIKey = [key copy]; } @@ -132,6 +133,11 @@ - (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(S [parameters setValue:@"true" forKey:@"sensor"]; [parameters setValue:[NSLocale preferredLanguages][0] forKey:@"language"]; + + if (googleMapsAPIKey) { + [parameters setValue:googleMapsAPIKey forKey:@"key"]; + } + [self addParametersToRequest:parameters]; self.state = SVGeocoderStateReady; From 4a14dd53dc24eab63d04338d9a5585a553ada8e9 Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Thu, 27 Mar 2014 17:28:55 -0300 Subject: [PATCH 5/8] Updated readme to new static googleMapsAPIKey. --- README.textile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.textile b/README.textile index 14af39b..0aa0707 100644 --- a/README.textile +++ b/README.textile @@ -62,12 +62,12 @@ SVPlacemark is now a simple NSObject. Here's a sample placemark returned for str There's also a @region@ (@MKCoordinateRegion@) property in case the returned placemark isn't a pinned location, and a @location@ (@CLLocation@) convenience property. -h2. Google Maps API Token +h2. Google Maps API Key -You can include a Google Maps API token by initializing the geocoder and setting it. You should use the default initializers to include the token. +You can include a Google Maps API Key by setting it before every request. If the key is not used the request will not be authenticated and request limits may apply.
-[geocoder setGoogleMapsToken:@"token"];
+[SVGeocoder setGoogleMapsAPIKey:@"KEY"];
 
h2. Credits From 4cbdfdc8e4d272fdf7e50621e200eaadeb0f650d Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Fri, 28 Mar 2014 15:01:42 -0300 Subject: [PATCH 6/8] Enabling ssl by default. --- SVGeocoder/SVGeocoder.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SVGeocoder/SVGeocoder.m b/SVGeocoder/SVGeocoder.m index d6e0550..bf34327 100644 --- a/SVGeocoder/SVGeocoder.m +++ b/SVGeocoder/SVGeocoder.m @@ -128,7 +128,7 @@ + (void)setGoogleMapsAPIKey:(NSString *)key { - (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters 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 = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/geocode/json"]]; [self.operationRequest setTimeoutInterval:kSVGeocoderTimeoutInterval]; [parameters setValue:@"true" forKey:@"sensor"]; From f5f73c04d358d5b84cc19193cf46c3b4a7b2dd36 Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Fri, 28 Mar 2014 15:04:40 -0300 Subject: [PATCH 7/8] Upgrading version. --- SVGeocoder.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SVGeocoder.podspec b/SVGeocoder.podspec index 9588016..3ca7c7e 100644 --- a/SVGeocoder.podspec +++ b/SVGeocoder.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SVGeocoder' - s.version = '0.2' + s.version = '0.3' s.license = 'MIT' s.platform = :ios s.summary = 'Simple Cocoa wrapper for the Google Geocoding Service.' From 3f5dd83539e3f4423a0aff286e8a83dc6a18f4a7 Mon Sep 17 00:00:00 2001 From: Julio Turolla Date: Fri, 28 Mar 2014 16:35:41 -0300 Subject: [PATCH 8/8] Updated readme to alert users that they need a browser google maps api key instead of an iOS key. --- README.textile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.textile b/README.textile index 0aa0707..48d37e7 100644 --- a/README.textile +++ b/README.textile @@ -66,6 +66,8 @@ h2. Google Maps API Key You can include a Google Maps API Key by setting it before every request. If the key is not used the request will not be authenticated and request limits may apply. +ATTENTION: You should generate a "browser key" (without any referrer) on Google API Console to use SVGecoder this way. iOS keys won't work as they are used in the official Google SDKs only, and they use the bundle identifier to validate the request. +
 [SVGeocoder setGoogleMapsAPIKey:@"KEY"];