Skip to content

Instantly share code, notes, and snippets.

@bstillitano
Created January 8, 2024 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstillitano/15dfa4af1d76974289a595bea44e27f9 to your computer and use it in GitHub Desktop.
Save bstillitano/15dfa4af1d76974289a595bea44e27f9 to your computer and use it in GitHub Desktop.
Async/Await GMSPlace Search
import GooglePlaces
func getAddresses(withQuery query: String) async -> [GMSPlace]? {
let client = GMSPlacesClient.shared()
let token = GMSAutocompleteSessionToken.init()
let filter = GMSAutocompleteFilter()
let predictions: [GMSAutocompletePrediction]? = await withCheckedContinuation { continuation in
client.findAutocompletePredictions(
fromQuery: query, filter: filter, sessionToken: token,
callback: { (results, error) in
guard error == nil else {
continuation.resume(returning: nil)
return
}
guard let results = results else {
continuation.resume(returning: nil)
return
}
continuation.resume(returning: results)
})
}
guard
let places: [GMSPlace?] = await predictions?.asyncMap({ prediction in
return await withCheckedContinuation { continuation in
client.lookUpPlaceID(prediction.placeID) { place, error in
continuation.resume(returning: place)
}
}
})
else { return nil }
return places.compactMap({ $0 })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment