ios – MKLocalSearch solely yields US outcomes, broaden search?


I’m utilizing MKLocalSearch to permit customers to seek for their metropolis. Nevertheless, once I attempt the code I solely obtain ends in the US. Additionally I obtain lots of outcomes that embody shops and so forth. I added the filters in func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) to do away with outcomes that aren’t cities. This works however I really feel like this will fail for sure locations. I attempted including the code under however that did not work. I would recognize any assist.

let worldRegion = MKCoordinateRegion(heart: CLLocationCoordinate2D(latitude: 0, longitude: 0),
                                       span: MKCoordinateSpan(latitudeDelta: 180, longitudeDelta: 360))
self.searchCompleter.area = worldRegion
import Basis
import Mix
import MapKit

class LocationService: NSObject, ObservableObject {

    enum LocationStatus: Equatable {
        case idle
        case noResults
        case isSearching
        case error(String)
        case outcome
    }

    @Revealed var queryFragment: String = ""
    @Revealed personal(set) var standing: LocationStatus = .idle
    @Revealed personal(set) var searchResults: [MKLocalSearchCompletion] = []

    personal var queryCancellable: AnyCancellable?
    personal let searchCompleter: MKLocalSearchCompleter!

    init(searchCompleter: MKLocalSearchCompleter = MKLocalSearchCompleter()) {
        self.searchCompleter = searchCompleter
        tremendous.init()
        self.searchCompleter.delegate = self
        
        queryCancellable = $queryFragment
            .obtain(on: DispatchQueue.foremost)
            .debounce(for: .milliseconds(250), scheduler: RunLoop.foremost, choices: nil)
            .sink(receiveValue: { fragment in
                self.standing = .isSearching
                if !fragment.isEmpty {
                    self.searchCompleter.queryFragment = fragment
                } else {
                    self.standing = .idle
                    self.searchResults = []
                }
        })
    }
}

extension LocationService: MKLocalSearchCompleterDelegate {
    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        self.searchResults = completer.outcomes.filter { end in
            if !outcome.title.accommodates(",") {
                return false
            }
            if outcome.title.rangeOfCharacter(from: CharacterSet.decimalDigits) != nil {
                return false
            }
            if outcome.subtitle.rangeOfCharacter(from: CharacterSet.decimalDigits) != nil {
                return false
            }
            return true
        }
        self.standing = completer.outcomes.isEmpty ? .noResults : .outcome
    }

    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        self.standing = .error(error.localizedDescription)
    }
}
import SwiftUI
import MapKit

struct locView: View {
    @StateObject var locationService = LocationService()
    var physique: some View {
        VStack {
            Type {
                Part(header: Textual content("Location Search")) {
                    ZStack(alignment: .trailing) {
                        TextField("Search", textual content: $locationService.queryFragment)
                        if locationService.standing == .isSearching {
                            Picture(systemName: "clock")
                                .foregroundColor(Coloration.grey)
                        }
                    }
                }
                Part(header: Textual content("Outcomes")) {
                    Checklist {
                        Group { () -> AnyView in
                            change locationService.standing {
                            case .noResults: return AnyView(Textual content("No Outcomes"))
                            case .error(let description): return AnyView(Textual content("Error: (description)"))
                            default: return AnyView(EmptyView())
                            }
                        }.foregroundColor(Coloration.grey)

                        ForEach(locationService.searchResults, id: .self) { completionResult in
                            Textual content(completionResult.title)
                        }
                    }
                }
            }
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles