MKLocalSearch没有MKMapView [英] MKLocalSearch WITHOUT MKMapView

查看:123
本文介绍了MKLocalSearch没有MKMapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to use MKLocalSearch to display results for a pre-defined string with results relevant to the users position, however all the examples I have seen so far require or use MKMapView to set the users location, and perhaps use the search bar to gather the text needed for the search.

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

I would simply like to perform the search on a predefined string and load the results in a tableview, without first having a map, is there a good example of how to do this?

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

Editing to add more detail, including the code I am currently trying to use. This code does not produce a table of results.

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

Further edit: Anna below has pointed out that the problem might be in UISearchDisplayController, however I have ripped the current code straight out of a working example project, so I really can't see where things are going wrong or why UISearchDisplayController is not showing the results.

头文件:

#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


推荐答案

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

I got this working in end the end, but without using the provided search table. I stored the response results and then implemented my own UITableView, assigning each result to a cell.

这篇关于MKLocalSearch没有MKMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆