'GMSAutocompleteViewController not working properly
I am trying to find a my address "Royal castle apartments A-block" In android device its listing in search but iOS device la not listing that above address and I tried setting country code as well nothing works.
I have added VC life cycle method
override func viewDidLoad() {
super.viewDidLoad()
containerView.alpha = alpha
containerView.layer.cornerRadius = cornerRadius
view.backgroundColor = backgroundColor.withAlphaComponent(backgroundOpacity)
shadowView.backgroundColor = backgroundColor.withAlphaComponent(backgroundOpacity)
self.mapView?.isMyLocationEnabled = true
mapView.settings.compassButton = true
mapView.settings.myLocationButton = true
//Location Manager code to fetch current location
self.locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
startAnimating(type: animationType)
// Do any additional setup after loading the view.
}
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
// Specify the place data types to return.
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(20) |
UInt(GMSPlaceField.placeID.rawValue) | UInt(GMSPlaceField.formattedAddress.rawValue) | UInt(GMSPlaceField.coordinate.rawValue))!
autocompleteController.placeFields = fields
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
var codeToFilter = "IN"
let countryLocale = NSLocale.current
let countryCode = countryLocale.regionCode
if let country = (countryLocale as NSLocale).displayName(forKey: NSLocale.Key.countryCode, value: countryCode!){
print(countryCode!, country)
if let cc = countryCode{
codeToFilter = cc
}
}
// Specify a filter.
let filter = GMSAutocompleteFilter()
filter.type = .address
// filter.country = codeToFilter
autocompleteController.autocompleteFilter = filter
// Display the autocomplete view controller.
present(autocompleteController, animated: true, completion: nil)
I have added delegates method given by place sdk
extension MapDragVC: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
// print("Place name: \(place.name)")
// print("Place ID: \(place.placeID)")
// print("Place attributions: \(place.attributions)")
// let coordinate = CLLocationCoordinate2DMake(13.067439, 80.237617)
self.mapView.clear()
self.addMarker(location:
place.coordinate)
self.geocodeCoordinates(location: place.coordinate)
print("Place name: \(place.formattedAddress)")
// if let formmattedAddress = place.formattedAddress{
// self.addressLbl.text = formmattedAddress
// }
// if let coordinates = place.coordinate{
//
// }
DispatchQueue.main.async {
self.dismiss(animated: true, completion: {
})
}
}
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)
DispatchQueue.main.async {
self.dismiss(animated: true, completion: {
})
}
}
// 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
}
}
As document i have added code https://developers.google.com/places/ios-sdk/autocomplete
Solution 1:[1]
You only need to change filter to no filter
let filter = GMSAutocompleteFilter()
filter.type = .noFilter
Happy Coding!
Solution 2:[2]
I'll see it try this.
let autocompleteController = GMSAutocompleteViewController()
override func viewDidLoad() {
super.viewDidLoad()
containerView.alpha = alpha
containerView.layer.cornerRadius = cornerRadius
view.backgroundColor = backgroundColor.withAlphaComponent(backgroundOpacity)
shadowView.backgroundColor = backgroundColor.withAlphaComponent(backgroundOpacity)
self.mapView?.isMyLocationEnabled = true
mapView.settings.compassButton = true
mapView.settings.myLocationButton = true
//Location Manager code to fetch current location
self.locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
startAnimating(type: animationType)
// Do any additional setup after loading the view.
autocompleteController.delegate = self
// Specify the place data types to return.
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(20) |
UInt(GMSPlaceField.placeID.rawValue) | UInt(GMSPlaceField.formattedAddress.rawValue) | UInt(GMSPlaceField.coordinate.rawValue))!
autocompleteController.placeFields = fields
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
var codeToFilter = "IN"
let countryLocale = NSLocale.current
let countryCode = countryLocale.regionCode
if let country = (countryLocale as NSLocale).displayName(forKey: NSLocale.Key.countryCode, value: countryCode!){
print(countryCode!, country)
if let cc = countryCode{
codeToFilter = cc
}
}
// Specify a filter.
let filter = GMSAutocompleteFilter()
filter.type = .address
// filter.country = codeToFilter
autocompleteController.autocompleteFilter = filter
// Display the autocomplete view controller.
present(autocompleteController, animated: true, completion: nil)
}
you must set the delegate inside the viewDidLoad Method, since there is not guaratee that the delegate is set with your aproach.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Akshaykumar Maldhure |
| Solution 2 | Yoel Jimenez del valle |
