How to extract street, city, etc. from GMSPlace Address Components

后端 未结 10 1905
栀梦
栀梦 2021-02-20 02:51

I\'m using Google Places API for iOS and can successfully retrieve nearby places and present the address as a string. What I\'m trying to do is extract address components such

10条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 03:54

    It's working in Swift 5 and iOS 13.3

    1. Create a function for showing GMSAutocompleteViewController

        func googlePlacesSearchVC() {
        
            let acController = GMSAutocompleteViewController()
            acController.delegate = self
            // Specify the place data types to return.
            let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
          UInt(GMSPlaceField.formattedAddress.rawValue)  |
                   UInt(GMSPlaceField.addressComponents.rawValue))!
        
            acController.placeFields = fields
            // Specify a filter.
            let filter = GMSAutocompleteFilter()
            filter.country = "IN"
            acController.autocompleteFilter = filter
    
            acController.secondaryTextColor = .darkGray
            acController.primaryTextColor = .lightGray
            acController.primaryTextHighlightColor = .black
            acController.tableCellBackgroundColor = .whiteThree
            acController.tableCellSeparatorColor = .lightGray
            // Display the autocomplete view controller.
            present(acController, animated: true) {
    
                let views = acController.view.subviews
                let subviewsOfSubview = views.first!.subviews
                let subOfNavTransitionView = subviewsOfSubview[1].subviews
                let subOfContentView = subOfNavTransitionView[1].subviews
                let searchBar = subOfContentView[0] as! UISearchBar
                searchBar.searchTextField.placeholder = "places_picker_hint_add_address".localized
                searchBar.searchBarStyle = .minimal
                searchBar.searchTextField.font = UIFont.screenTitle16Pt
                searchBar.searchTextField.backgroundColor = UIColor.white
                searchBar.searchTextField.leftView?.tintColor = .darkGray
                searchBar.delegate?.searchBar?(searchBar, textDidChange: "")
            }
       }
    

    2. Call that function where ever you need, for ex:-

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

    3. Call GMSAutoCompleteViewControllerDelegates Methods. Here will get all details like Street, City, etc. from GMSPlace Address Components

    extension ViewController {
    
          // Handle the user's selection.
          func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
            
                // Show HouseAndFlat
                if place.name?.description != nil {
                    
                    yourTxtField.text = place.name?.description ?? ""
                }
    
                // Show latitude
                if place.coordinate.latitude.description.count != 0 {
                    var latitude = place.coordinate.latitude
                }
                // Show longitude
                if place.coordinate.longitude.description.count != 0 {
                    selectedLongitude = place.coordinate.longitude
                }
    
                // Show AddressComponents
                if place.addressComponents != nil {
                    
                    for addressComponent in (place.addressComponents)! {
                       for type in (addressComponent.types){
    
                           switch(type){
                               case "sublocality_level_2":
                                   yourTxtField.text = addressComponent.name
                               case "sublocality_level_1":
                                    yourTxtField.text = addressComponent.name
                                case "administrative_area_level_2":
                                    yourTxtField.text = addressComponent.name
    
                                case "administrative_area_level_1":
                                     yourTxtField.text = addressComponent.name
                                case "country":
                                    yourTxtField.text = addressComponent.name
    
                                case "postal_code":
                                     yourTxtField.text = addressComponent.name
                           default:
                               break
                           }
                       }
                   }
                }
                dismiss(animated: true, completion: nil)
          }
    
          func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
                // TODO: handle the error.
                print("Error: ", error.localizedDescription)
          }
    
          // User canceled the operation.
          func wasCancelled(_ viewController: GMSAutocompleteViewController) {
                dismiss(animated: true, completion: nil)
          }
    }
    

    I have found an answer from this link

提交回复
热议问题