I’ve some code under that searches for cities on the planet. Solely town title and nation is returned. Is there any manner I can tweak the code to additionally get the coordinates of every outcome? I do know I can use a geocoder to get the coordinates however I hoped there is a easier manner since im already utilizing MKLocalSearch.
class CitySearchViewModel: NSObject, ObservableObject, MKLocalSearchCompleterDelegate {
@Revealed var searchQuery: String = ""
@Revealed var searchResults: [CityResult] = []
non-public var searchCompleter: MKLocalSearchCompleter!
override init() {
tremendous.init()
searchCompleter = MKLocalSearchCompleter()
searchCompleter.delegate = self
searchCompleter.resultTypes = .tackle
}
func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
let outcomes = getCityList(outcomes: completer.outcomes)
let ultimate = Array(Set(outcomes))
DispatchQueue.fundamental.async {
self.searchResults = ultimate
}
}
func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) { }
func performSearch() {
searchCompleter.queryFragment = searchQuery
}
struct CityResult: Hashable {
var metropolis: String
var nation: String
}
non-public func getCityList(outcomes: [MKLocalSearchCompletion]) -> [CityResult] {
var searchResults: [CityResult] = []
for lead to outcomes {
let titleComponents = outcome.title.parts(separatedBy: ", ")
let subtitleComponents = outcome.subtitle.parts(separatedBy: ", ")
buildCityTypeA(titleComponents, subtitleComponents) { place in
if !place.metropolis.isEmpty && !place.nation.isEmpty {
searchResults.append(CityResult(metropolis: place.metropolis, nation: place.nation))
}
}
buildCityTypeB(titleComponents, subtitleComponents) { place in
if !place.metropolis.isEmpty && !place.nation.isEmpty {
searchResults.append(CityResult(metropolis: place.metropolis, nation: place.nation))
}
}
}
return searchResults
}
non-public func buildCityTypeA(_ title: [String], _ subtitle: [String], _ completion: @escaping ((metropolis: String, nation: String)) -> Void) {
var metropolis: String = ""
var nation: String = ""
if title.depend > 1 && subtitle.depend >= 1 {
metropolis = title.first!
nation = subtitle.depend == 1 && subtitle[0] != "" ? subtitle.first! : title.final!
}
completion((metropolis, nation))
}
non-public func buildCityTypeB(_ title: [String], _ subtitle: [String], _ completion: @escaping ((metropolis: String, nation: String)) -> Void) {
var metropolis: String = ""
var nation: String = ""
if title.depend >= 1 && subtitle.depend == 1 {
metropolis = title.first!
nation = subtitle.final!
}
completion((metropolis, nation))
}
}