如何在Google Maps iOS SDK中筛选自动完成的国家?

8

目前,它在每个国家查询,但我希望它只在新加坡查询。

我找到了一个stackoverflow的解决方案(如何使用Google自动完成地点API ios sdk获取特定国家的结果?),但我不知道在哪里放置代码。

这是代码,我使用全屏自动完成解决方案。

import UIKit
import GoogleMaps

class OnlyLocationViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
                // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func clickMe(sender: UIButton) {
        let autocompletecontroller = GMSAutocompleteViewController()
        autocompletecontroller.delegate = self
        self.presentViewController(autocompletecontroller, animated: true, completion: nil)


    }

}

extension OnlyLocationViewController: GMSAutocompleteViewControllerDelegate{

    func viewController(viewController: GMSAutocompleteViewController, didAutocompleteWithPlace place: GMSPlace) {
        print("Place name: ", place.name)
        print("Place address: ", place.formattedAddress)
        print("Place attributions: ", place.attributions)

        self.dismissViewControllerAnimated(true, completion: nil)
    }


    func viewController(viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: NSError) {
        // To handle error
        print(error)

    }
    func wasCancelled(viewController: GMSAutocompleteViewController) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    func didRequestAutocompletePredictions(viewController: GMSAutocompleteViewController) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    }

    func didUpdateAutocompletePredictions(viewController: GMSAutocompleteViewController) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    }


}

过滤器代码应该放在哪里?


1个回答

23

试着使用这个

@IBAction func clickMe(sender: UIButton) {
        let autocompletecontroller = GMSAutocompleteViewController()
        autocompletecontroller.delegate = self
        let filter = GMSAutocompleteFilter()
        filter.type = .Establishment  //suitable filter type
        filter.country = "SG"  //appropriate country code
        autocompletecontroller.autocompleteFilter = filter
        self.presentViewController(autocompletecontroller, animated: true, completion: nil)
    }

关于此内容的更多信息,请访问以下链接:https://developers.google.com/places/ios-sdk/autocomplete#filter_results_by_country。而https://dev59.com/0FwZ5IYBdhLWcg3wPOT2是一个关于此主题的好讨论。 - Wimukthi Rajapaksha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接