【问题标题】:Google Places API Autocomplete on iOSiOS 上的 Google Places API 自动完成功能
【发布时间】:2013-10-22 14:06:27
【问题描述】:

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

我按照 Google 的“教程”进行实施,Places API 已启用,API 密钥具有正确的捆绑 ID,我还使用浏览器 ID 进行了测试,但没有成功。

不过,我很确定这与密钥有关。奇怪的是,在应用程序的 Android 版本上,该服务将与浏览器键一起使用。显然,在浏览器上,它也可以使用该密钥。

这是我尝试过的两个键:

这是我的实现代码,使用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 系统,它在您输入完整地址时效果很好,但在输入一半的短语时效果不佳。这一点都不好,我很想用谷歌的API。

【问题讨论】:

    标签: ios google-places-api


    【解决方案1】:

    我明白了!

    参数sensor 应该接收@"true"或@"false",而不是@(true)或@(false)。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这是一个很好的建议,但它并没有解决我的问题。我仍然得到REQUEST_DENIED。用两个键再试一次。
      【解决方案3】:

      https://maps.googleapis.com/maps/api/geocode/json?address="%@","%@"&sensor=false,yourAddress,your c

      【讨论】:

      • 请不要只发布链接答案。只需将链接的基本部分放在您的答案中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 2014-05-01
      • 2017-10-02
      相关资源
      最近更新 更多