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?
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: