ios - 如何限制 Google Places Autocomplete 不包含地址?

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

如何限制 Google Places Autocomplete 不包含地址?我目前有我的设置

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 将我的结果限制为仅包括州城市、邮政编码、国家/地区。我不想在自动完成 Controller 中显示地址。当我这样做时,它只显示国家和城市,我无法输入邮政编码。我将如何提供它?我不太确定我遗漏了什么或下一步要做什么。任何建议将不胜感激。

最佳答案

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

func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
    var placeClient = GMSPlacesClient()
    let filter = GMSAutocompleteFilter()
    filter.country = "USA"
    filter.type = .region

    resultsViewController.autocompleteFilter = filter 

这会将搜索限制为不包括地址。在尝试 .city 时,这消除了很多。希望这对将来的人有所帮助。

关于ios - 如何限制 Google Places Autocomplete 不包含地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327631/

相关文章:

ios - 是否可以在iOS中比较两个AES加密字符串?

ios - 为未使用的静态更新 Objective-C 语法与 Swift 等效

ios - 如何在obj c单例类中限制对alloc,init的调用

ios - UIView 返回错误大小

java - 新的 Google Map Api for Java 应用程序,在 JPanel 中添加 map

android - 除非您更新 Google Play 服务(通过 Bazaar),否则此应用将无法运行

iphone - 导航栏和 View 之间的分隔符 - iOS 7

swift - 向图像添加触摸手势并执行代码

ios - UIView 内的 UITableView

javascript - Json 字符串转 Google map 标记