8

Apple introduced the MKLocalSearchCompleter and MKLocalSearchCompletion in iOS 9.3. I am trying to implement it. It becomes a two step process 1) enter partial term -> full search text is generated. 2) User selects one of these to search for the actual location.

The question is if I search for 200 townsend, it gives me a list of locations but it is till treated as suggestion by the app. How can we identify if it is a MKMampItem or suggestion?

iosCurator
  • 4,356
  • 2
  • 21
  • 25

1 Answers1

12

One way to do this is to initialize a MKLocalSearchRequest with a MKLocalSearchCompletion.

let request = MKLocalSearchRequest(completion: completion)

You can then initialize a MKLocalSearch with a MKLocalSearchRequest.

let search = MKLocalSearch(request: request)

You can then start the search which has a completion handler with a MKLocalSearchResponse? and NSError?. The MKLocalSearchResponse? will have an array of MKMapItem's.

Full example:

let request = MKLocalSearchRequest(completion: completion)
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler { (response: MKLocalSearchResponse?, error: NSError?) in
    if let error = error {
        // do something with "error"
    }
    else if let mapItems = response?.mapItems {
        // do something with "mapItems"
    }
}
jherg
  • 1,696
  • 2
  • 13
  • 15
  • Download the Apple Official Sample Code here that explains how to make the `MKLocalSearchRequest` from from an `MKLocalSearchCompletion`: https://developer.apple.com/documentation/mapkit/searching_for_nearby_points_of_interest – ChrisJF Dec 10 '18 at 23:02
  • Does this risk getting rate limited? I have experience some problems when making many calls in debug – lolelo Jan 24 '22 at 08:33