我正在尝试将位置搜索列表显示到TableView中,首先,这是可能的吗?
如果是这样,我该怎么做呢?
我收集列表的代码是:
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的数组中。
其内容如下:
NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]);是所有位置的完整列表,格式为:
Name: Strada Café, Placemark title: 94 Rue du Temple, 75003 Paris, France其内容如下:
NSLog(@"%@", gLoc);是数组,所有位置的格式为:
"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)",我对如何继续下去感到困惑。理想情况下,我希望将这些信息转换为标题的名称和副标题的地址,有没有办法以这样的方式操作数据来实现这一点?
发布于 2017-08-23 15:01:40
尝试以下代码:
#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);
}结果:

https://stackoverflow.com/questions/45828778
复制相似问题