MKMapItem 到 TableView
MKMapItem to TableView
我正在尝试将位置搜索列表显示到 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)",
我不知道如何继续。我希望将这些信息理想地转换为名称作为标题和地址作为副标题,有没有办法以这种方式操作数据来实现这一点?
试试下面的代码:
#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);
}
结果:
我正在尝试将位置搜索列表显示到 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)",
我不知道如何继续。我希望将这些信息理想地转换为名称作为标题和地址作为副标题,有没有办法以这种方式操作数据来实现这一点?
试试下面的代码:
#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);
}
结果: