如何限制Google商家信息自动填充功能不包含地址?

时间:2018-01-18 18:01:58

标签: ios swift google-maps autocomplete google-places-api

如何限制Google商家信息自动填充功能不包含地址?我目前有我的设置

class FilterVC: UIViewController, GMSAutocompleteViewControllerDelegate {

// MARK: SEARCH BAR

@IBAction func searchBarAction(_ sender: Any) {
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self
    placeAutocomplete(resultsViewController: autocompleteController)
    UINavigationBar.appearance().barTintColor = UIColor.white
    UINavigationBar.appearance().tintColor = UIColor.hiGreyishBrownTwo
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.hiGreyishBrownTwo]
    present(autocompleteController, animated: true, completion: nil)
}
func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
    var placeClient = GMSPlacesClient()
    let filter = GMSAutocompleteFilter()
    filter.type = .city
    filter.country = "USA"
    resultsViewController.autocompleteFilter = filter

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
        DataService.instance.place = place
        FillAddress(place: place)
        fillAddressForm()

        print(DataService.instance._address_line1)
         print(DataService.instance._city)
         print(DataService.instance._postalCode)
         print(DataService.instance._state)
         print(DataService.instance._country)
        DataService.instance.addressLabel = place.formattedAddress
        dismiss(animated: true, completion: nil)
}

func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    print("ERROR \(error) Autocomplete")
}

我尝试使用GMSAutocompleteFilter将结果限制为仅包含州城市,邮政编码,国家/地区。我不想在自动完成控制器中显示地址。当我这样做时,它只显示国家和城市,我无法输入邮政编码。我该如何提供?我不太清楚我错过了什么,或者下一步要采取的措施是什么。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:3)

因为我找到了一个解决方案,所以我打算将其关闭,但是为了防止有人遇到类似的问题,一个非常简单的解决方法是将过滤器类型设置为区域。

function oddCount(n){ 
  if (n === 0) return 0;
  return n % 2 === 0 ? oddCount(n - 1) : oddCount(n - 1) + 1;
}

这会将搜索限制为不包含地址。在尝试.city时,那就消除了很多。希望这能帮助将来的某个人。