我正在使用MKLocalSearch执行地图搜索。我的问题是MKMapItems在响应中的顺序。例如,搜索我目前所在的城市,首先会返回一些与城市名称相关的业务,我将不得不向下滚动一段时间才能找到实际的城市。我使用region属性MKLocalSearchRequest将搜索集中在近区域。
我的问题是:
不确定是否相关,但以下是搜索的代码:
-(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];
}
}];
}发布于 2014-02-18 00:27:27
MKLocalSearchRequest只接受naturalLanguageQuery和region作为参数,所以您可以“执行只返回地址的搜索吗?”-不行。
但是,在请求完成后,筛选列表是非常容易的。如果您想从列表中筛选出业务,那么一个简单的NSPredicate就可以完成这项工作。
NSPredicate *noBusiness = [NSPredicate predicateWithFormat:@"business.uID == 0"];
NSMutableArray *itemsWithoutBusinesses = [response.mapItems mutableCopy];
[itemsWithoutBusinesses filterUsingPredicate:noBusiness];发布于 2014-10-25 01:39:42
与使用MKLocalSearch不同,您可以使用CLGeocoder并执行反向geo代码查询:
CLGeocoder *geo = [[CLGeocoder alloc] init];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
// look at the placemarks and grab the locality out of them to get the city name
}];https://stackoverflow.com/questions/21364355
复制相似问题