Reputation: 7959
Over the last few days I've been struggling to get Google Places autocomplete to work on my app, and no matter what I do, I always get the REQUEST_DENIED
error.
I followed Google's "tutorial" for the implementation, Places API is enabled, API key has the correct bundle ID, I also tested with a Browser ID, with no success.
I am pretty sure it has something to do with the key, though. Curiously, on the Android version of the app, the service will work with the Browser Key. And on the browser, obviously, it works with that key too.
These are the two keys I experimented with:
And this is my implementation code, using AFNetworking
:
NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/autocomplete/"];
NSDictionary *params = @{@"input" : [input stringByReplacingOccurrencesOfString:@" " withString:@"+"],
@"location" : [NSString stringWithFormat:@"%f,%f", searchCoordinate.latitude, searchCoordinate.longitude],
@"sensor" : @(true),
// @"language" : @"pt_BR",
// @"types" : @"(regions)",
// @"components" : @"components=country:br",
@"key" : GOOGLE_API_KEY};
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
[httpClient getPath:@"json"
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *JSON = [[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil] dictionaryWithoutNulls];
if (completion) {
completion(JSON[@"predictions"]);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *errorResponse) {
NSLog(@"[HTTPClient Error]: %@ for URL %@", [errorResponse localizedDescription], [[[operation request] URL] path]);
}];
I know there are some questions like this one here, but some are old, and say that Places API does not work on Android or iOS, which clearly is not the case anymore, since Google itself publishes examples on both platforms, as seen on Places API page: https://developers.google.com/places/documentation/autocomplete
A workaround I'm currently using is Apple's GeoCoding system, which works good when you type the full address, but is terrible with half-typed phrases. This is not good at all, I'd really like to use Google's API.
Upvotes: 2
Views: 7203
Reputation: 11
https://maps.googleapis.com/maps/api/geocode/json?address="%@","%@"&sensor=false,yourAddress,your c
Upvotes: 1
Reputation: 7959
I got it!
The paramenter sensor
should receive either @"true"
or @"false"
, and not @(true)
or @(false)
.
For the record, the API key used is indeed the Browser Key, and not an iOS key.
Upvotes: 2
Reputation: 1806
You should init httpClient
with base URL @"https://maps.googleapis.com/"
and get path /maps/api/place/autocomplete/json
. Base URL - host only, it does not take other parts of path, so in your case you get request to URL "https://maps.googleapis.com/json".
Upvotes: 1