【问题标题】:google search places autocomplete specific to country in ios谷歌搜索在 ios 中放置特定于国家/地区的自动完成功能
【发布时间】:2013-12-05 08:46:46
【问题描述】:

我正在开发 Google Places AutoComplete 服务,我需要过滤特定国家/地区的地点并找到 this

直到我传递一个参数 components=country:se 来过滤自动完成字段,它的响应是 REQUEST_DENIED

NSMutableString *url = [NSMutableString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&components=country:se&sensor=%@&key=%@",
                                                             [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                                                             SPBooleanStringForBool(sensor), key];

我需要自动完成功能来提供针对国家/地区的建议。

谢谢

【问题讨论】:

  • 您确定密钥正确吗?你为什么使用 NSMutableString 而不是 NSString?之后您是否在字符串中添加了更多内容?您能否在请求完成之前调试或 NSLog NSURL 以检查它是否包含您所期望的内容?顺便说一句,您使用的是从 Google 获得的服务器密钥吗?
  • 是的,我使用了从 Google API 控制台生成的服务器密钥。是的,我将一些参数附加到 URL github.com/spoletto/SPGooglePlacesAutocomplete/blob/master/…。这个网址我正在日志中“maps.googleapis.com/maps/api/place/autocomplete/…
  • 链接末尾有一个双引号字符。当我删除它时,当我在我的网络浏览器中运行它时,调用就会起作用。
  • 是的@Hunkpapa 我做了同样的事情,我在浏览器上测试过它,所以我做了同样的事情,如下所示。 Thanxs NSMutableString *url = [NSMutableString stringWithFormat:@"maps.googleapis.com/maps/api/place/autocomplete/…", [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], SPBooleanStringForBool(sensor)];
  • 好的,但是多余的双引号是从哪里来的呢?它必须添加到某处。在从方法 googleURLString 返回之前添加一个 NSLog 语句,这样您就可以准确地看到 url 包含的内容

标签: ios


【解决方案1】:

这对我来说很好。 您可以通过添加地理区域的最左上角和右下角位置点来添加区域的边界来设置它。

就像我用的美国一样。

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:CLLocationCoordinate2DMake(52.00, 148.00)
                                                                   coordinate:CLLocationCoordinate2DMake(18.00, -71.00)];

GMSPlacesClient * placesClient = [[GMSPlacesClient alloc] init];

[placesClient autocompleteQuery:fieldValue bounds:bounds filter:nil
                       callback:^(NSArray *results, NSError *error)
 {
     if (error != nil)
     {
         NSLog(@"Autocomplete error %@", [error localizedDescription]);
         return;
     }
     else
     {
         [self.autoComplete removeAllObjects];
         [self.AddressPlaceID removeAllObjects];
         for (GMSAutocompletePrediction* result in results)
         {

             CustomAddress * parseAddress = [[CustomAddress alloc] init];
             NSString * adString = [parseAddress removeCountryFromAddress:result.attributedFullText.string];

             [self.autoComplete addObject:adString];
             [self.AddressPlaceID addObject:result.placeID];

         }
         [self.autoCompleteTable reloadData];
     }
 }];

但您仍然可以获得其他位置结果。它会首先选择您在 GMCoordinateBounds 中定义的地理范围

【讨论】:

    【解决方案2】:

    为什么不使用 iOS SDK 来获取特定国家/地区的结果?关注这个answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2017-12-23
      • 1970-01-01
      • 2018-08-12
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多