2014-01-26 51 views
4

我正在使用MKLocalSearch執行地圖搜索。我的問題是在響應中的MKMapItems的排序。例如,搜索我目前所在的城市將首先返回一些與城市名稱相關的業務,我將不得不向下滾動一段時間才能找到實際的城市。我正在使用MKLocalSearchRequestregion屬性將搜索集中在附近區域。過濾/訂購MKLocalSearch響應

我的問題是:

  1. 你能效果,其中的響應返回給你的命令?例如,地圖應用會在剛剛輸入第一個字母后列出我的城市。沒有POI或企業上榜?
  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]; 
     } 
    }]; 
} 
+0

請參見下面的鏈接可能是它會幫助你。 http://stackoverflow.com/questions/14950896/showing-nearby-restaurants-in-mkmap-view –

回答

4

MKLocalSearchRequest只接受naturalLanguageQuery和區域作爲參數,這樣你就可以「進行搜索,只有返回地址? 「 - 編號

但是,在請求完成後過濾列表非常簡單。如果您想從列表中篩選出企業,簡單的NSPredicate可以完成這項工作。

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

雖然這在技術上解決了這個問題,但它在實踐中通常是無用的。由於MKLocalSearch僅返回10個結果,因此在搜索後應用過濾器通常不會得到任何結果或一個或兩個結果。例如,如果您搜索城市名稱,您將永遠不會收回,因爲前10個搜索結果始終是商戶。 – Joel

+0

我很困惑,蘋果公司爲返回的商家提供了哪些ID? – thedeveloper3124

+0

事實證明,這是一個私人的Apple框架 – thedeveloper3124

0

比使用MKLocalSearch相反,你可以使用CLGeocoder,做一個反向地理編碼查詢:

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 

}];