Reputation: 6394
I am using MKLocalSearch
to perform a map search. My problem is the ordering of the MKMapItems in the response. For example, a search of the city that I am currently in would first return a number of businesses related to the city name and I would have to scroll down for a while to find the actual city. I am using the region
property of MKLocalSearchRequest
to focus the search in the near area.
My questions are:
Not sure if relevant or not, but here is the code for the search:
-(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];
}
}];
}
Upvotes: 4
Views: 2769
Reputation: 711
Rather than using MKLocalSearch, you could use CLGeocoder and do a reverse geo code query:
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
}];
Upvotes: 0
Reputation: 5088
MKLocalSearchRequest only accepts naturalLanguageQuery and region as parameters, so can you "perform a search that only returns addresses?"- No.
However, it's very easy to filter the list after the request has finished. If you'd like to filter out businesses from the list a simple NSPredicate does the job.
NSPredicate *noBusiness = [NSPredicate predicateWithFormat:@"business.uID == 0"];
NSMutableArray *itemsWithoutBusinesses = [response.mapItems mutableCopy];
[itemsWithoutBusinesses filterUsingPredicate:noBusiness];
Upvotes: 5