Search code examples
iosswiftgoogle-places-apigoogle-places-autocomplete

GMSPlace Auto Complete Always return same Place coordinate


the return of auto Autocomplete always give the same value.

  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
      print("Place name: \(place.name)")
      print("Place coordinate: \(place.coordinate)")
    dismiss(animated: true, completion: nil)
  }

this the value I always get:

CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0)

this is the code I use to call the searchViewController for address:

func showSearchViewController() {
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self
    
    // Specify the place data types to return.
    let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue))
    autocompleteController.placeFields = fields
    
    // Specify a filter.
    let filter = GMSAutocompleteFilter()
    filter.type = .address
    autocompleteController.autocompleteFilter = filter
    
    // Display the autocomplete view controller.
    present(autocompleteController, animated: true, completion: nil)
}

Solution

  • to resolve this you need to specify in Fields GMSPlaceField the Data you want.

    for example if you wanna the name and coordinates you need to put:

    let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(GMSPlaceField.coordinate.rawValue))
    autocompleteController.placeFields = fields