Skip to content

Commit

Permalink
Merge pull request #31 from jturolla/master
Browse files Browse the repository at this point in the history
Including Google Maps API Token
  • Loading branch information
samvermette committed May 6, 2014
2 parents 780dc53 + 291c7e7 commit 85ef3da
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Demo/Classes/SVGeocoderAppAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ 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 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.

<pre>
[SVGeocoder setGoogleMapsAPIKey:@"KEY"];
</pre>

h2. Credits

Expand Down
2 changes: 1 addition & 1 deletion SVGeocoder.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SVGeocoder'
s.version = '0.1'
s.version = '0.3'
s.license = 'MIT'
s.platform = :ios
s.summary = 'Simple Cocoa wrapper for the Google Geocoding Service.'
Expand Down
8 changes: 8 additions & 0 deletions SVGeocoder/SVGeocoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ 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*)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;
Expand All @@ -48,6 +55,7 @@ typedef void (^SVGeocoderCompletionHandler)(NSArray *placemarks, NSHTTPURLRespon
- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block;

- (void)start;

- (void)cancel;

@end
18 changes: 15 additions & 3 deletions SVGeocoder/SVGeocoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
};

typedef NSUInteger SVGeocoderState;

static NSString *googleMapsAPIKey;

@interface NSString (URLEncoding)
- (NSString*)encodedURLParameterString;
Expand All @@ -42,11 +42,13 @@ @interface SVGeocoder ()
- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block;

- (void)addParametersToRequest:(NSMutableDictionary*)parameters;

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

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

- (void)callCompletionBlockWithResponse:(id)response error:(NSError *)error;

@end
Expand Down Expand Up @@ -99,6 +101,12 @@ + (SVGeocoder *)reverseGeocode:(CLLocationCoordinate2D)coordinate completion:(SV
return geocoder;
}

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

googleMapsAPIKey = [key copy];

}

#pragma mark - Public Initializers

- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block {
Expand All @@ -116,7 +124,6 @@ - (SVGeocoder*)initWithAddress:(NSString*)address completion:(SVGeocoderCompleti
return [self initWithParameters:parameters completion:block];
}


- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block {
NSString *bounds = [self createBoundsStringFromRegion:region];

Expand Down Expand Up @@ -154,11 +161,16 @@ - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region com
- (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"];
[parameters setValue:[NSLocale preferredLanguages][0] forKey:@"language"];

if (googleMapsAPIKey) {
[parameters setValue:googleMapsAPIKey forKey:@"key"];
}

[self addParametersToRequest:parameters];

self.state = SVGeocoderStateReady;
Expand Down

0 comments on commit 85ef3da

Please sign in to comment.