Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 516
iosCurator Avatar asked May 10 '16 06:05

iosCurator


1 Answers

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"
    }
}
like image 131
jherg Avatar answered Nov 06 '22 20:11

jherg