如何在iOS的GMSAutocompleteResultsViewController中仅过滤学校?

时间:2018-09-05 09:57:08

标签: ios swift xcode google-places-api

我正在使用GMSAutocompleteResultsViewController在iOS中搜索位置。我写下地点的名称,并根据关键字获得结果。但是问题是我只想搜索学校。因此,如果我输入的是学校的任何地名,那么我应该出现在结果中,否则不会出现。

GMSAutocompleteFilter 仅显示了一些过滤器,例如营业地点,地区,城市,地址等。但是在android中,它已将过滤器作为学校

下面是我用于搜索的代码。

class SearchLocationController: DIBaseController {

    var resultsViewController: GMSAutocompleteResultsViewController?
    var searchController: UISearchController?
    var resultView: UITextView?

    override func viewDidLoad() {
        super.viewDidLoad()

        resultsViewController = GMSAutocompleteResultsViewController()
        resultsViewController?.delegate = self

        searchController = UISearchController(searchResultsController: resultsViewController)
        searchController?.searchResultsUpdater = resultsViewController

        let filter = GMSAutocompleteFilter()
        filter.type = .establishment
        resultsViewController?.autocompleteFilter = filter

        // Put the search bar in the navigation bar.
        searchController?.searchBar.sizeToFit()
        navigationItem.titleView = searchController?.searchBar

        // When UISearchController presents the results view, present it in
        // this view controller, not one further up the chain.
        definesPresentationContext = true
        // Prevent the navigation bar from being hidden when searching.
        searchController?.hidesNavigationBarDuringPresentation = false
    }
}

// Handle the user's selection.
extension SearchLocationController: GMSAutocompleteResultsViewControllerDelegate {
    func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                           didAutocompleteWith place: GMSPlace) {
        searchController?.isActive = false
        // Do something with the selected place.

    }

    func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                           didFailAutocompleteWithError error: Error){
        // TODO: handle the error.
        print("Error: ", error.localizedDescription)
    }

    // Turn the network activity indicator on and off again.
    func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

    func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }
}

1 个答案:

答案 0 :(得分:1)

显然,在iOS SDK中,受支持的位置类型仅为def to_representation(self, instance): ret = super().to_representation(instance) ret['year_in_school'] = instance.get_year_in_school_display() return ret geocodeaddressestablishmentregion

有关更多详细信息,请参阅this