Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter autocomplete country in Google maps ios sdk?

Currently , it queries in every country, but I want it to only query in Singapore

I found a stackoverflow solution How to get country specific result with Google autocomplete place api ios sdk?, but couldn't figure out where do i put the code.

Here's the code, I use the full screen solution for autocomplete.

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
    }


}

Where should I put the filter code?

like image 313
airsoftFreak Avatar asked Jun 10 '16 17:06

airsoftFreak


1 Answers

Try using this

@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)
    }
like image 111
Sanman Avatar answered Sep 27 '22 21:09

Sanman