Skip to content

Instantly share code, notes, and snippets.

@aheze
Created June 16, 2022 23:13
Show Gist options
  • Save aheze/dc3d29b4893727d5a24d842c0bb799a0 to your computer and use it in GitHub Desktop.
Save aheze/dc3d29b4893727d5a24d842c0bb799a0 to your computer and use it in GitHub Desktop.
Get multiple map locations from a query string
enum Shared {
static func getMatchingLocations(from query: String, region: MKCoordinateRegion) async -> (locations: [MKMapItem], region: MKCoordinateRegion?) {
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = query
request.region = region
let search = MKLocalSearch(request: request)
return await withCheckedContinuation { continuation in
search.start { response, _ in
guard let response = response else {
continuation.resume(returning: ([], nil))
return
}
let locations = response.mapItems
continuation.resume(returning: (locations, response.boundingRegion))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment