0

I am trying to display a list of location searchs into a TableView, first of all is this possible?

If so how would i go about this?

My code to gather the list is:

MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
[searchRequest setNaturalLanguageQuery:@"Cafe"];

CLLocationCoordinate2D userCenter = CLLocationCoordinate2DMake(48.8566, 2.3522);
MKCoordinateRegion userRegion = MKCoordinateRegionMakeWithDistance(userCenter, 15000, 15000);
[searchRequest setRegion:userRegion];

MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

    if (!error) {
        NSMutableArray *gLoc = [NSMutableArray array];
        for (MKMapItem *mapItem in [response mapItems]) {
            [gLoc  addObject:mapItem.placemark];
            NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]);

In the above example I am searching for "Cafe" in Paris, and storing the information into an Array called gLoc.

The contents of :

NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]);

Is a complete list of all locations formatted as:

Name: Strada Café, Placemark title: 94 Rue du Temple, 75003 Paris, France

The contents of :

NSLog(@"%@",  gLoc);

Is array with all locations formatted as:

"Strada Caf\U00e9, 94 Rue du Temple, 75003 Paris, France @ <+48.86220020,+2.35731150> +/- 0.00m, region CLCircularRegion (identifier:'<+48.86220020,+2.35731150> radius 49.91', center:<+48.86220020,+2.35731150>, radius:49.91m)",

I'm stumped on how to continue. I was looking to turn this information into the name as a title and address as subtitle ideally, is there a way to manipulate the data in such a way to achieve this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
tysco
  • 25
  • 5

1 Answers1

0

Try the following code:

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation ViewController
{
    NSMutableArray *gLoc;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

    // Do any additional setup after loading the view, typically from a nib.
    MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
    [searchRequest setNaturalLanguageQuery:@"Cafe"];

    CLLocationCoordinate2D userCenter = CLLocationCoordinate2DMake(48.8566, 2.3522);
    MKCoordinateRegion userRegion = MKCoordinateRegionMakeWithDistance(userCenter, 15000, 15000);
    [searchRequest setRegion:userRegion];

    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

        if (!error) {
            gLoc = [NSMutableArray array];
            for (MKMapItem *mapItem in [response mapItems]) {
                [gLoc  addObject:mapItem.placemark];
                NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]);
            }
            [tableView reloadData];
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    return [gLoc count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:cellIdentifier];
    }

    MKPlacemark *placemark = gLoc[indexPath.row];
    cell.textLabel.text = placemark.name;
    NSArray *lines = placemark.addressDictionary[ @"FormattedAddressLines"];
    cell.detailTextLabel.text = [lines componentsJoinedByString:@","];

    return cell;
}

#pragma mark - UITableViewDelegate

// when user tap the row, what action you want to perform
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"selected %d row", indexPath.row);
}

Result:

enter image description here

Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • Sorry for the delayed response, was out of town. That worked beautifully thank you very much!! – tysco Aug 24 '17 at 12:24
  • Sorry, i do have one question @KosukeOgawa, this works fine on my test project but when importing the code into my application I get the "Thread 1: EXC_BAD_ACCESS" on: "MKPlacemark *placemark = gLoc[indexPath.row];" everything is exactly the same, copied from my working test project, but i cannot understand where the issue is coming from. – tysco Aug 24 '17 at 13:07
  • Also; the error is also sometimes returning: "-[__NSDictionaryM objectAtIndexedSubscript:]" – tysco Aug 24 '17 at 13:27
  • Your `gLoc` is not `NSMutableArray`, but `NSMutableDictionary`. Isn't it? Check `NSLog(@"%@", [gLoc className],` – Kosuke Ogawa Aug 24 '17 at 14:42
  • Yes, the gLoc is returning a class of NSMutableArray – tysco Aug 24 '17 at 14:48
  • just wanted to let you know that the problem was that my ARC was set to NO. – tysco Aug 24 '17 at 16:10
  • If your ARC is set to NO, you have to use `gLoc = [[NSMutableArray alloc] init];` instead of `gLoc = [NSMutableArray array];` – Kosuke Ogawa Aug 24 '17 at 23:47
  • Thanks for everything! Works like a charm – tysco Aug 25 '17 at 00:12