2017-08-23 2 views
0

J'essaie d'afficher une liste de recherche d'emplacement dans un TableView, tout d'abord est-ce possible?MKMapItem to TableView

Si oui, comment j'irais à ce sujet?

Mon code pour recueillir la liste est:

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]); 

Dans l'exemple ci-dessus, je suis à la recherche de « Café » à Paris, et stocker les informations dans un tableau appelé Gloc.

Le contenu de:

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

une liste complète de tous les lieux formatés comme:

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

Le contenu de:

NSLog(@"%@", gLoc); 

est un tableau avec tous les emplacements formatés :

"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)", 

Je suis perplexe sur la façon de continuer. Je cherchais à transformer cette information en un titre et une adresse sous-titre idéalement, y a-t-il un moyen de manipuler les données de manière à y parvenir?

Répondre

0

Essayez le code suivant:

#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); 
} 

Résultat:

enter image description here

+0

Désolé pour la réponse tardive, était hors de la ville. Cela a fonctionné magnifiquement merci beaucoup !! – tysco

+0

Désolé, j'ai une question @KosukeOgawa, cela fonctionne très bien sur mon projet de test, mais lors de l'importation du code dans mon application, je reçois le "Thread 1: EXC_BAD_ACCESS" sur: "MKPlacemark * placemark = gLoc [indexPath.row] " Tout est exactement le même, copié à partir de mon projet de test de travail, mais je ne peux pas comprendre d'où vient le problème. – tysco

+0

Aussi; l'erreur renvoie aussi parfois: "- [__ NSDictionaryM objectAtIndexedSubscript:]" – tysco