Google has always been somewhat ahead of the game in map search – for example in suggesting “search completion” strings when we start typing in places, points of interests or partial addresses in Maps. But Apple is catching up – and catching up fast.
In iOS 9.3, Apple introduced a very useful class for developers called MKLocalSearchCompleter – one that is sure to swing several iOS dev teams to lean towards using Apple Maps in lieu of Google Maps for implementing the search completion functionality in their apps.
The basic idea of search completion is to be able to type in a partial string in a search box and get several suggestions in real time as we are typing in. The user can then tap on one of the suggested options – and be taken to that location on the map view.
Here is an example of what is now possible using the MKLocalSearchCompleter class :
To implement this functionality in your apps, just follow these simple steps:
1.
Add the delegate to your interface.
@interface YourMapVC : UIViewController < .. ,MKLocalSearchCompleterDelegate> { }
2.
Add a couple of new properties:
. . @property (strong, nonatomic) MKLocalSearchCompleter *completer; @property(nonatomic, readonly, strong) NSArray <MKLocalSearchCompletion *> *results; . .
3.
Initialize the class somewhere in your implementation:
. . completer = [[MKLocalSearchCompleter alloc] init]; completer.delegate = self; completer.filterType = MKSearchCompletionFilterTypeLocationsAndQueries; . .
4.
Add these delegate methods and use the results returned in your app:
- (void) completerDidUpdateResults:(MKLocalSearchCompleter *)completer { for (MKLocalSearchCompletion *completion in completer.results) { NSLog(@"------ %@",completion.description); } } - (void) completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error { NSLog(@"Completer failed with error: %@",error.description); }
In the above code, I am printing out the results returned in the array – we can use these to update a table below the search box in real time.
I was quite impressed with how responsive and relevant this search functionality is in a real app. For additional details, check out the official Apple Docs.
Nice work, Apple!