首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不带MKMapView的MKLocalSearch

不带MKMapView的MKLocalSearch
EN

Stack Overflow用户
提问于 2014-04-13 02:39:43
回答 1查看 3.2K关注 0票数 3

我想使用MKLocalSearch来显示预定义字符串的结果,其中包含与用户位置相关的结果,但是到目前为止,我看到的所有示例都需要或使用MKMapView来设置用户位置,也许还需要使用搜索栏来收集搜索所需的文本。

我只是想在一个预定义的字符串上执行搜索,并将结果加载到一个表视图中,而不需要先有一个映射,有没有一个很好的例子来说明如何做到这一点?

编辑以添加更多细节,包括我当前尝试使用的代码。此代码不会生成结果表。

进一步编辑:安娜下面指出问题可能出在UISearchDisplayController中,但是我直接从一个工作的示例项目中提取了当前的代码,所以我真的看不出哪里出了问题,也看不出为什么UISearchDisplayController没有显示结果。

头文件:

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface CallACabViewController : UIViewController <CLLocationManagerDelegate,     UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate>
{
    CLLocationManager *locationManager;
    MKLocalSearch *localSearch;
    MKLocalSearchResponse *results;
}

-(IBAction)closeButtonClicked:(id)sender;

@end

实现文件:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [self.searchDisplayController setDelegate:self];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

#pragma mark - LocationUpdating

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];
    CLLocation *oldLocation;
    if (locations.count > 1) {
        oldLocation = [locations objectAtIndex:locations.count-2];
    } else {
        oldLocation = nil;
    }
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
    MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.00, 1500.00);
    [self performSearch:userLocation];

}

#pragma mark - Search Methods


-(void)performSearch:(MKCoordinateRegion)aRegion
{
    // Cancel any previous searches.
    [localSearch cancel];

    [locationManager stopUpdatingLocation];

    // Perform a new search.
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = @"taxi";
    request.region = aRegion;

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    localSearch = [[MKLocalSearch alloc] initWithRequest:request];

    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){

        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        if (error != nil) {
            [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil)
                                        message:[error localizedDescription]
                                       delegate:nil
                              cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
            return;
        }

        if ([response.mapItems count] == 0) {
            [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
                                        message:nil
                                       delegate:nil
                              cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
            return;
        }

        results = response;

        [self.searchDisplayController.searchResultsTableView reloadData];
    }];

    NSLog(@"DEBUG");


}


#pragma mark - TableView Delegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [results.mapItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *IDENTIFIER = @"SearchResultsCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
    }

    MKMapItem *item = results.mapItems[indexPath.row];

    cell.textLabel.text = item.name;
    cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.searchDisplayController setActive:NO animated:YES];
}

-(IBAction)closeButtonClicked:(id)sender
{
    [locationManager stopUpdatingLocation];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
EN

回答 1

Stack Overflow用户

发布于 2014-04-19 15:34:09

我最后让它工作了,但没有使用提供的搜索表。我存储了响应结果,然后实现了自己的UITableView,将每个结果分配给一个单元格。

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

https://stackoverflow.com/questions/23034527

复制
相关文章

相似问题

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