Wednesday, 10 August 2016

AutoPlace completion using MKLocalSearchCompleter in iOS using Objective C

We have seen Google API for autocompletion of places . But in iOS 9.3 apple introduced MKLocalSearchCompleter .

Today we will learn how to use this to search places nearby our Location.

1. create a new Xcode Project Lets say MKLocalSearchControllerDemo.
2. Now go to storyBoard and take one searchbar, tableview and one mapView.

3. Make Proper IBOutLet Connection and add MapKit Framework in your project.

#import "ViewController.h"
#import <Mapkit/MapKit.h>

@interface ViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

4. Now go to YourView ViewDidLoad and write this code.

@implementation ViewController
    MKLocalSearch *localSearch;
    MKLocalSearchResponse *results;

- (void)viewDidLoad {
    [super viewDidLoad];
    CLLocationCoordinate2D india = CLLocationCoordinate2DMake(17.4622,78.3568);
    [self.mapView setRegion: MKCoordinateRegionMakeWithDistance(india, 2000, 2000)];
    [self.mapView setMapType: MKMapTypeStandard];
    // Do any additional setup after loading the view, typically from a nib.


Note. Here I am fixing the coordinate to my place. But You can use CLLocationManager to get the current location of the user.
I am also fixing the radius in which it will search, you can increase decrease according to your need.

5. Now just write the following code .

#pragma mark -  tableView Delagate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [results.mapItems count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *IDENTIFIER = @"SearchResultsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
    MKMapItem *item = results.mapItems[indexPath.row];
    cell.textLabel.text =  cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",,item.placemark.addressDictionary[@"Street"]];
   // cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];
    return cell;

#pragma mark - Search delegate methods

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = searchText;
    request.region = self.mapView.region;
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if (error != nil) {
        if ([response.mapItems count] == 0) {
        for (MKMapItem *item in response.mapItems)
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            annotation.coordinate = item.placemark.coordinate;
            annotation.title      =;
            annotation.subtitle   = item.placemark.title;
            [self.mapView addAnnotation:annotation];
        results = response;
        [_tableView reloadData];

6. Now run the code and see the output. It will show you the places name and it will show draw annotation in tha mapView.

You can share your comment and reviews if you liked it.

Enjoy Coding :)

No comments:

Post a Comment

Ads Inside Post

Blogger Widget