Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place auto complete is not working after updating to version 3.0.2 - Google iOS sdk

I had the implementation of place auto completion using GMSAutocompleteResultsViewController was working fine so far. But when I updated the sdk to 3.0.2 stopped working. If I revert to version 2.7.0 start working.

I have gone through the migrationguide not getting what I am missing. I checked the examples didn't observe any changes. Would anyone point me in the right direction to make it work again?

class PlaceAutoCompleteController: BaseViewController {

    //MARK: - Property declaration
    private var resultsViewController = GMSAutocompleteResultsViewController()
    private lazy var searchController = UISearchController(searchResultsController: resultsViewController)
    private var viewModel: PlaceTypeAheadViewModel
    var storeSelectionDelegate: StoreSelectionDelegate?

    //MARK: - Life cycle
    init(withViewModel aviewModel: PlaceTypeAheadViewModel) {
        viewModel = aviewModel
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupProperties()
    }
}

//MARK: - Property setup
extension PlaceAutoCompleteController {

    /// Setting up view default properties
    func setupProperties() {

        resultsViewController.view.backgroundColor = UIColor.white
        resultsViewController.tableCellBackgroundColor = .white
        resultsViewController.delegate = self

        // Specify the place data types to return.
//        let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
//            UInt(GMSPlaceField.placeID.rawValue))!
//        resultsViewController.placeFields = fields

        let filter      = GMSAutocompleteFilter()

        //suitable filter type
        filter.type     = .establishment
        filter.country  = viewModel.getCountryId()
        resultsViewController.autocompleteFilter = filter

        searchController.searchResultsUpdater = resultsViewController

        // Put the search bar in the navigation bar.
        searchController.searchBar.sizeToFit()
        searchController.searchBar.placeholder = "Search places"
        let searchBgView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44))
        searchBgView.addSubview(searchController.searchBar)
        view.addSubview(searchBgView)

        // Prevent the navigation bar from being hidden when searching.
        searchController.hidesNavigationBarDuringPresentation = false

        // This makes the view area include the nav bar even though it is opaque.
        // Adjust the view placement down.
        extendedLayoutIncludesOpaqueBars = false
        edgesForExtendedLayout = []

    }
}

// Handle the user's selection.
extension PlaceAutoCompleteController: GMSAutocompleteResultsViewControllerDelegate {

    func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                           didAutocompleteWith place: GMSPlace) {
        searchController.isActive = false
        AppManager.setPlace(withPlaceId: place.placeID ?? "" , name: place.name ?? "" , latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
        openStoreSelection()
    }

    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
    }

    /// Open store selection screen with selected country
    func openStoreSelection() {
        navigateToStoreSelection()
    }
}



Thanks in advance.

like image 816
Alex Avatar asked Oct 16 '22 06:10

Alex


1 Answers

In your Google Cloud project:

  1. Make sure you enabled "Places API". The old one "Places SDK for iOS" does not work with 3.0.0 version of SDK.

enter image description here

  1. Update API restrictions for your API key with "Places API" instead of "Places SDK for iOS"

enter image description here

  1. Setup billing details for your Google Cloud project
like image 157
coconata Avatar answered Oct 20 '22 17:10

coconata