How to identify the MKMapItem from MKLocalSearchCompletion object in iOS9.3?

2.2k views Asked by At

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?

1

There are 1 answers

2
jherg On

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"
    }
}