iOS上的Google Places API自动完成功能 [英] Google Places API Autocomplete on iOS

查看:159
本文介绍了iOS上的Google Places API自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天,我一直在努力使Google地方信息自动完成功能可以在我的应用上正常工作,无论我做什么,我总是会收到REQUEST_DENIED错误.

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.

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

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.

我非常确定它与密钥有关.奇怪的是,在该应用的Android版本上,该服务将与浏览器密钥一起使用.显然,在浏览器上,它也可以使用该键.

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:

这是我使用AFNetworking的实现代码:

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]);
              }];

我知道这里有一些类似的问题,但是有些问题已经过时了,并说Places API不能在Android或iOS上运行,这显然不再适用了,因为Google本身在两个平台上都发布了示例,例如在Places API页面上可见: https://developers.google.com/places/documentation/autocomplete

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

我当前正在使用的解决方法是Apple的GeoCoding系统,当您键入完整的地址时,该系统很好用,但对于半键入的短语却很糟糕.这一点都不好,我真的很想使用Google的API.

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.

推荐答案

我明白了!

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

The paramenter sensor should receive either @"true"or @"false", and not @(true)or @(false).

出于记录,使用的API密钥确实是浏览器密钥,而不是iOS密钥.

For the record, the API key used is indeed the Browser Key, and not an iOS key.

这篇关于iOS上的Google Places API自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆