-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGooglePlacesAPI.m
More file actions
96 lines (73 loc) · 4.12 KB
/
Copy pathGooglePlacesAPI.m
File metadata and controls
96 lines (73 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// GooglePlacesAutoComplete.m
// Henry
//
// Created by Samuel Kitono on 9/03/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "GooglePlacesAPI.h"
#import "NSString+SBJSON.h"
@implementation GooglePlacesAPI
@synthesize delegate,isUsingBiasRegion;
-(void) requestAutoCompleteFinished:(ASIHTTPRequest *) request{
NSLog(@"Request autocomplete %@",[request responseString]);
NSArray * predictions = [[[request responseString] JSONValue] objectForKey:@"predictions"];
if ([delegate respondsToSelector:@selector(googlePlacesFinishedAutoCompleteRequest:withResults:)]) {
[delegate googlePlacesFinishedAutoCompleteRequest:self withResults:predictions];
}else if([delegate respondsToSelector:@selector(googlePlacesFinishedAutoCompleteRequest:withResultsGooglePlaceAutoComplete:)]){
NSMutableArray * googlePlaceAutoCompleteArray = [NSMutableArray new];
for (NSDictionary * dictResult in predictions) {
GooglePlaceAutoComplete * autoComplete = [[GooglePlaceAutoComplete alloc] initWithDictionary:dictResult];
[googlePlaceAutoCompleteArray addObject:autoComplete];
[autoComplete release];
}
[delegate googlePlacesFinishedAutoCompleteRequest:self withResultsGooglePlaceAutoComplete:googlePlaceAutoCompleteArray];
[googlePlaceAutoCompleteArray release];
}
}
-(void) requestGooglePlaceDetailFinished:(ASIHTTPRequest *) request{
NSLog(@"REquest detail is %@",[request responseString]);
NSDictionary * dictResult = [[[request responseString] JSONValue] objectForKey:@"result"];
if ([delegate respondsToSelector:@selector(googlePlacesFinishedDetail:withDetail:)]) {
[delegate googlePlacesFinishedDetail:self withDetail:dictResult];
}else if([delegate respondsToSelector:@selector(googlePlacesFinishedDetail:withDetailGooglePlace:)]){
GooglePlace * googlePlace = [[GooglePlace alloc] initWithDictionary:dictResult];
[delegate googlePlacesFinishedDetail:self withDetailGooglePlace:googlePlace];
[googlePlace release];
}
}
-(void) requestFailed:(ASIHTTPRequest *)request{
NSLog(@"GOOGLE PLACES REQUEST FAILED with response %@",[request responseString]);
}
-(void) setBiasingWithLocationCoordinate:(CLLocationCoordinate2D)centerCoordinate andRadiusInMeters:(float)radius{
locationCoordinate = centerCoordinate;
radiusSearch = radius;
}
-(void) returnAutoCompletePlacesWithSearchString:(NSString *)inputSearchString{
NSArray * addressArray = [inputSearchString componentsSeparatedByString:@" "];
NSString * searchString = [addressArray objectAtIndex:0];
for (int i = 1; i<[addressArray count]; i++) {
NSString * string = [addressArray objectAtIndex:i];
searchString = [NSString stringWithFormat:@"%@+%@",searchString,string];
}
NSString * urlString;
if (isUsingBiasRegion) {
urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=geocode&location=%f,%f&radius=%f&sensor=true&key=%@&",searchString,locationCoordinate.latitude,locationCoordinate.longitude,radiusSearch,GOOGLE_API_KEY];
}else{
urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=geocode&sensor=true&key=%@&",searchString,GOOGLE_API_KEY];
}
NSURL * url = [NSURL URLWithString:urlString];
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];
request.delegate = self;
[request setDidFinishSelector:@selector(requestAutoCompleteFinished:)];
[request startAsynchronous];
}
-(void) getGooglePlaceDetailWithReference:(NSString *)reference{
NSString * urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/details/json?reference=%@&sensor=true&key=%@",reference,GOOGLE_API_KEY];
NSURL * url = [NSURL URLWithString:urlString];
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];
request.delegate = self;
[request setDidFinishSelector:@selector(requestGooglePlaceDetailFinished:)];
[request startAsynchronous];
}
@end