首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapItem到TableView

MKMapItem到TableView
EN

Stack Overflow用户
提问于 2017-08-23 08:22:19
回答 1查看 93关注 0票数 0

我正在尝试将位置搜索列表显示到TableView中,首先,这是可能的吗?

如果是这样,我该怎么做呢?

我收集列表的代码是:

代码语言:javascript
复制
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]);

在上面的示例中,我在巴黎搜索"Cafe“,并将信息存储到一个名为gLoc的数组中。

其内容如下:

代码语言:javascript
复制
NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]);

是所有位置的完整列表,格式为:

代码语言:javascript
复制
Name: Strada Café, Placemark title: 94 Rue du Temple, 75003 Paris, France

其内容如下:

代码语言:javascript
复制
NSLog(@"%@",  gLoc);

是数组,所有位置的格式为:

代码语言:javascript
复制
"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)",

我对如何继续下去感到困惑。理想情况下,我希望将这些信息转换为标题的名称和副标题的地址,有没有办法以这样的方式操作数据来实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-23 15:01:40

尝试以下代码:

代码语言:javascript
复制
#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);
}

结果:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45828778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档