Domanda

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:

  1. Can you effect the order in which the responses are returned to you? For instance, the Maps app will list my city on top after just entering the first letter. How come no POI:s or businesses were listed on top?
  2. Is there a way of eliminating businesses completely from the search, and only get addresses in response?

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];
        }
    }];
}
È stato utile?

Soluzione

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];

Altri suggerimenti

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

 }];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top