如何在Google地图ios sdk中过滤自动填充国家/地区?

时间:2016-06-10 17:08:31

标签: ios swift google-maps google-maps-sdk-ios

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

我找到了一个stackoverflow解决方案How to get country specific result with Google autocomplete place 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 个答案:

答案 0 :(得分:20)

尝试使用此

@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)
    }

希望有所帮助:)