The autocomplete service in the Places SDK for iOS returns place predictions in response to user search queries. As the user types, the autocomplete service returns suggestions for places such as businesses, addresses and points of interest.
Here i will pick the location use the results controller.
Use a results controller when you want more control over the text input UI. The results controller dynamically toggles the visibility of the results list based on input UI focus.
Steps to add a results controller to your app and get the result
1-> Add below pod with your pod file
1 2 |
pod 'GooglePlaces pod 'GooglePlacePicker' |
2-> Add below code in app delegate file
1 2 |
GMSServices.provideAPIKey("YOUR_API_KEY "); GMSPlacesClient.provideAPIKey("YOUR_API_KEY"); |
3-> Create a GMSAutocompleteResultsViewController
1 |
var resultsViewController: GMSAutocompleteResultsViewController? |
4-> Implement the GMSAutocompleteResultsViewControllerDelegate protocol in the parent view controller.
5-> Take UISearchController object, as a result, view controller argument.
6-> Handle the user’s selection in the didAutocompleteWithPlace
delegate method.
7-> Add below key with your info-plist file
1 2 3 |
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key><string>$(PRODUCT_NAME) requires GPS to get current locations</string> <key>NSLocationAlwaysUsageDescription</key><string>$(PRODUCT_NAME) requires GPS to get current location</string> <key>NSLocationWhenInUseUsageDescription</key><string>$(PRODUCT_NAME) requires GPS to get current location</string> |
Add Below code with your view controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import UIKit import GooglePlaces class LocationViewController: UIViewController { var resultsViewController: GMSAutocompleteResultsViewController? var searchController: UISearchController? override func viewDidLoad() { super.viewDidLoad() self.title = "Select Addres".localised resultsViewController = GMSAutocompleteResultsViewController() resultsViewController?.delegate = self searchController = UISearchController(searchResultsController: resultsViewController) searchController?.searchResultsUpdater = resultsViewController let subView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 45.0)) subView.addSubview((searchController?.searchBar)!) view.addSubview(subView) definesPresentationContext = true // Do any additional setup after loading the view. } @IBAction func cancelAct(_ sender: Any) { self.dismiss(animated: true, completion: nil) } } extension LocationViewController: GMSAutocompleteResultsViewControllerDelegate { func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didAutocompleteWith place: GMSPlace) { searchController?.isActive = false // Do something with the selected place. print("Place Address: \(place.formattedAddress)") searchController?.searchBar.text = place.formattedAddress self.dismiss(animated: true, completion: nil) } 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(forResultsController resultsController: GMSAutocompleteResultsViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = true } func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = false } } |
The output is:- Place Address: Delhi, India
Conclusion
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.