Skip to content

Commit 85ef3da

Browse files
committed
Merge pull request #31 from jturolla/master
Including Google Maps API Token
2 parents 780dc53 + 291c7e7 commit 85ef3da

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

Demo/Classes/SVGeocoderAppAppDelegate.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2525
// Add the view controller's view to the window and display.
2626
[self.window addSubview:viewController.view];
2727
[self.window makeKeyAndVisible];
28+
29+
[SVGeocoder setGoogleMapsAPIKey:@"KEY"];
2830

2931
return YES;
3032
}

README.textile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ SVPlacemark is now a simple NSObject. Here's a sample placemark returned for str
6262

6363
There's also a @region@ (@MKCoordinateRegion@) property in case the returned placemark isn't a pinned location, and a @location@ (@CLLocation@) convenience property.
6464

65+
h2. Google Maps API Key
66+
67+
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.
68+
69+
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.
70+
71+
<pre>
72+
[SVGeocoder setGoogleMapsAPIKey:@"KEY"];
73+
</pre>
6574

6675
h2. Credits
6776

SVGeocoder.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'SVGeocoder'
3-
s.version = '0.1'
3+
s.version = '0.3'
44
s.license = 'MIT'
55
s.platform = :ios
66
s.summary = 'Simple Cocoa wrapper for the Google Geocoding Service.'

SVGeocoder/SVGeocoder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,21 @@ typedef void (^SVGeocoderCompletionHandler)(NSArray *placemarks, NSHTTPURLRespon
3232

3333
@interface SVGeocoder : NSOperation
3434

35+
// Set static Google Maps API Key for all requests.
36+
// The key will be included in the request if it's
37+
// not nil.
38+
+ (void)setGoogleMapsAPIKey:(NSString*)key;
39+
3540
+ (SVGeocoder*)geocode:(NSString *)address completion:(SVGeocoderCompletionHandler)block;
41+
3642
+ (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block;
3743
+ (SVGeocoder*)geocode:(NSString *)address components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;
3844
+ (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;
3945

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

4248
- (SVGeocoder*)initWithAddress:(NSString *)address completion:(SVGeocoderCompletionHandler)block;
49+
4350
- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block;
4451
- (SVGeocoder*)initWithAddress:(NSString *)address components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;
4552
- (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region components:(NSDictionary *)components completion:(SVGeocoderCompletionHandler)block;
@@ -48,6 +55,7 @@ typedef void (^SVGeocoderCompletionHandler)(NSArray *placemarks, NSHTTPURLRespon
4855
- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(SVGeocoderCompletionHandler)block;
4956

5057
- (void)start;
58+
5159
- (void)cancel;
5260

5361
@end

SVGeocoder/SVGeocoder.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
};
2121

2222
typedef NSUInteger SVGeocoderState;
23-
23+
static NSString *googleMapsAPIKey;
2424

2525
@interface NSString (URLEncoding)
2626
- (NSString*)encodedURLParameterString;
@@ -42,11 +42,13 @@ @interface SVGeocoder ()
4242
- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block;
4343

4444
- (void)addParametersToRequest:(NSMutableDictionary*)parameters;
45+
4546
- (void)finish;
4647
- (NSString*)createComponentsStringFromDictionary:(NSDictionary *)components;
4748
- (NSString*)createBoundsStringFromRegion:(CLRegion *)region;
4849

4950
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
51+
5052
- (void)callCompletionBlockWithResponse:(id)response error:(NSError *)error;
5153

5254
@end
@@ -99,6 +101,12 @@ + (SVGeocoder *)reverseGeocode:(CLLocationCoordinate2D)coordinate completion:(SV
99101
return geocoder;
100102
}
101103

104+
+ (void)setGoogleMapsAPIKey:(NSString *)key {
105+
106+
googleMapsAPIKey = [key copy];
107+
108+
}
109+
102110
#pragma mark - Public Initializers
103111

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

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

@@ -154,11 +161,16 @@ - (SVGeocoder*)initWithAddress:(NSString *)address region:(CLRegion *)region com
154161
- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block {
155162
self = [super init];
156163
self.operationCompletionBlock = block;
157-
self.operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/geocode/json"]];
164+
self.operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/geocode/json"]];
158165
[self.operationRequest setTimeoutInterval:kSVGeocoderTimeoutInterval];
159166

160167
[parameters setValue:@"true" forKey:@"sensor"];
161168
[parameters setValue:[NSLocale preferredLanguages][0] forKey:@"language"];
169+
170+
if (googleMapsAPIKey) {
171+
[parameters setValue:googleMapsAPIKey forKey:@"key"];
172+
}
173+
162174
[self addParametersToRequest:parameters];
163175

164176
self.state = SVGeocoderStateReady;

0 commit comments

Comments
 (0)