How to search addresses in MKMap using MKLocalSearch

743 views Asked by At

I'm trying to search through the addresses using MKLocalSearch, but it only comes up with business and not the exact address like e.g Maps (Apple's App).

I hope u guys can help me out - thanks! :)

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = query;
request.region = self.mapView.region;

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.localSearch = [[MKLocalSearch alloc] initWithRequest:request];

[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if (error != nil) {
        [[[UIAlertView alloc] initWithTitle:@"Map Error"
                                    message:[error description]
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
        return;
    }

    self.results = response;

`

1

There are 1 answers

0
Mihado On

In your results array, which is of type MKMapItem you need to configure the cells to give you the address when the tableView presents itself. Here's a quick example.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let IDENTIFIER = "CellID"
    let cell = tableView.dequeueReusableCellWithIdentifier(IDENTIFIER, forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...
    var item: MKMapItem = results[indexPath.row]

    cell.textLabel?.text = item.placemark.name
    cell.detailTextLabel?.text = item.placemark.addressDictionary["Street"] as? String

    return cell
}

This is all done in your cellForRowAtIndexPath method that belongs to the UITableViewDataSource protocol.

Hope that helps, and best of luck to you!