【问题标题】:MKLocalSearch WITHOUT MKMapView没有 MKMapView 的 MKLocalSearch
【发布时间】:2014-04-12 18:39:43
【问题描述】:

我想使用 MKLocalSearch 来显示预定义字符串的结果以及与用户位置相关的结果,但是到目前为止我看到的所有示例都需要或使用 MKMapView 来设置用户位置,并且可能使用搜索栏来收集搜索所需的文本。

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

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

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

头文件:

#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

实现文件:

- (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

【问题讨论】:

  • MKLocalSearch 根本不需要地图。只需给它一个字符串并使用你喜欢的结果。
  • 它是否显示警报视图之一(“错误”或“无结果”)?如果response 包含项目但搜索表视图为空,则问题可能出在 UISearchDisplayController 而不是 MKLocalSearch。同样,不需要地图,但在您请求的区域中可能真的没有任何结果。
  • 不,它从不显示 UIAlerts,如果我在计时器上执行搜索,我可以看到结果实例不是 nil,并且有可以显示的 mapItems,所以我同意你的评估这是 UISearchDisplayController 的问题。
  • 也许 UISearchDisplayController 出于某种原因隐藏了表格视图。不过,您不必使用 UISearchDisplayController。另外,你可以放一个 NSLog(@"response = %@", response);直接在搜索完成处理程序块中检查响应(不需要计时器)。

标签: ios uitableview mkmapview mklocalsearch


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多