2013-10-22 69 views
2

在过去几天里,我一直在努力让Google Places自动完成功能在我的应用上运行,无论我做什么,我总是得到REQUEST_DENIED错误。iOS上的Google Places API自动完成功能

我跟着谷歌的“教程”实施,启用Places API,API密钥有正确的包ID,我也用浏览器ID测试过,但没有成功。不过,我很确定它与密钥有关。奇怪的是,在Android版本的应用程序中,该服务将与浏览器密钥一起工作。显然,在浏览器上,它也可以使用该键。

这是两个键我有尝试:

Google APIs Console

,这是我实现代码,使用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]); 
       }]; 

我知道这里有一些像这样的问题,但有些已经过时了,并且说Places API在Android或iOS上不起作用,这显然不再适用,因为Google本身在两个平台上发布了示例,如Places API页面上所示:https://developers.google.com/places/documentation/autocomplete

我目前使用的解决方法是Apple的GeoCoding系统,当您输入完整地址时效果很好,但对于半键式短语而言很糟糕。这并不好,我真的很喜欢使用Google的API。

回答

2

我明白了!

的paramenter sensor应该接受或@"true"@"false",而不是@(true)@(false)

为了记录,使用的API密钥确实是浏览器密钥,而不是iOS密钥。

1

您应该初始化为httpClient,并使用基本URL @"https://maps.googleapis.com/"并获取路径/maps/api/place/autocomplete/json。基本网址 - 仅限主机,它不占用路径的其他部分,因此在您的情况下,您会收到URL“https://maps.googleapis.com/json”的请求。

+0

这是一个好建议遵循的,但它并没有解决我的问题。我仍然得到'REQUEST_DENIED'。用两把钥匙再试一次。 – Guilherme

相关问题