MKLocalSearch how do I avoid results which have subtitle "Search Nearby"?

Viewed 741

I'm following this article: https://www.peteralt.com/blog/mapkit-location-search-with-swiftui/

And I've set up a LocationSearchService like this:

extension LocationSearchService: MKLocalSearchCompleterDelegate {
    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        self.searchResults = completer.results
        self.status = completer.results.isEmpty ? .noResults : .result
    }

    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        self.status = .error(error.localizedDescription)
    }
}

class LocationSearchService: NSObject, ObservableObject {

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

    @Published var queryFragment: String = ""
    @Published private(set) var status: LocationStatus = .idle
    @Published private(set) var searchResults: [MKLocalSearchCompletion] = []

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

    init(searchCompleter: MKLocalSearchCompleter = MKLocalSearchCompleter()) {
        self.searchCompleter = searchCompleter
        super.init()
        self.searchCompleter.delegate = self

        queryCancellable = $queryFragment
            .receive(on: DispatchQueue.main)
            .debounce(for: .milliseconds(250), scheduler: RunLoop.main, options: nil)
            .sink(receiveValue: { fragment in
                self.status = .isSearching
                if !fragment.isEmpty {
                    self.searchCompleter.queryFragment = fragment
                } else {
                    self.status = .idle
                    self.searchResults = []
                }
        })
    }
}

Unfortunately, I get values like this with "Search Nearby" in them. How do I filter this out?

enter image description here

2 Answers

I ran into the same issue.

I was able to fix it by specifying that I only want addresses and points of interest in the following way when setting up the MKLocalSearchCompleter.

searchCompleter.resultTypes = MKLocalSearchCompleter.ResultType([.address, .pointOfInterest])

I'm working on a similar issue. The MKLocalSearchCompleter class includes a property resultTypes which, according to documentation, allows you to choose between:

  • query
  • pointOfInterest
  • address

So it seems that you want to set the completer to exclude the result type "query," something like:

completer.resultTypes = [.address, .pointOfInterest] ** this isn't working code

I have found, however, no useful way to use this property. My solution has been to use a string filter to remove results include the string "Search Nearby":

- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer {
    
    NSMutableArray *suggestions = [[NSMutableArray alloc] init];
    NSArray* results = completer.results;
    
    for (MKLocalSearchCompletion *place in results)
    {
        if ([@"Search Nearby" isEqualToString:place.subtitle]) {
            continue;
        }
        [suggestions addObject:place];
    }
    ... // do something with suggestions
}

or in Swift:

var suggestions : [MKLocalSearchCompletion] = []
for place in results {
    if place.subtitle == "Search Nearby" { continue }
    resultsToReturn.add(place)
} 

I really dislike this solution, but it is working for me for now.

Related