ios - 过滤/排序 MKLocalSearch 响应

标签 ios iphone mapkit mklocalsearch

我正在使用 MKLocalSearch 执行 map 搜索。我的问题是响应中 MKMapItems 的排序。例如,搜索我当前所在的城市会首先返回一些与城市名称相关的商家,我必须向下滚动一段时间才能找到实际的城市。我正在使用 MKLocalSearchRequestregion 属性将搜索集中在附近区域。

我的问题是:

  1. 你能影响回复给你的顺序吗?例如, map 应用程序会在输入第一个字母后将我所在的城市列在顶部。为什么没有 POI:s 或企业列在顶部?
  2. 有没有一种方法可以从搜索中完全删除商家,而只获取地址作为响应?

不确定是否相关,但这是搜索代码:

-(void)issueLocalSearchLookup:(NSString *)searchString {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.location.coordinate, 30000, 30000);
    self.localSearchRequest = [[MKLocalSearchRequest alloc] init];
    self.localSearchRequest.region = region;
    self.localSearchRequest.naturalLanguageQuery = searchString;
    self.localSearch = [[MKLocalSearch alloc] initWithRequest:self.localSearchRequest];

    [self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        if(error){
            NSLog(@"LocalSearch failed with error: %@", error);
            return;
        } else {
            for(MKMapItem *mapItem in response.mapItems){
                [self.data addObject:mapItem];
            }
            [self.searchDisplayController.searchResultsTableView reloadData];
        }
    }];
}

最佳答案

MKLocalSearchRequest 只接受 naturalLanguageQuery 和 region 作为参数,所以你能“执行只返回地址的搜索吗?”- 不能。

但是,在请求完成后过滤列表非常容易。如果您想从列表中过滤掉企业,一个简单的 NSPredicate 就可以完成这项工作。

NSPredicate *noBusiness = [NSPredicate predicateWithFormat:@"business.uID == 0"];
NSMutableArray *itemsWithoutBusinesses = [response.mapItems mutableCopy];
[itemsWithoutBusinesses filterUsingPredicate:noBusiness];

关于ios - 过滤/排序 MKLocalSearch 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21364355/

相关文章:

ios - Xcode 在本地通知后显示特定 View

ios - 在 UITableview 中绘制带外部阴影的边框

ios - 在 iOS 中收到 APNS 时打开 View Controller

iphone - Objective-C:无法将图像写入文档目录

iphone - 简单,没有多余的装饰,在 iPhone 中获取 LAT/LON

ios - ViewDidLoad 方法再次被调用

iphone - 为什么 CLLocationManager 委托(delegate)在 iPhone SDK 4.0 中没有被调用?

swift 4 只删除一个注释(不是全部)

iphone - 兴趣点 API/Web 服务

ios - indexpath.row 从 1 而不是 0 开始