MKLocalSearch를 사용하여 사용자 위치와 관련된 결과가있는 미리 정의 된 문자열에 대한 결과를 표시하고 싶습니다. 그러나 지금까지 본 모든 예에서는 MKMapView를 사용하여 사용자 위치를 설정하고 아마도 검색 막대를 사용하여 검색에 필요한 텍스트를 수집하십시오.MKMapView가없는 MKLocalSearch
미리 정의 된 문자열에 대한 검색을 수행하고지도를 처음 만들지 않고 결과를 tableview에로드하는 것이 좋을 것입니다.이 작업을 수행하는 좋은 예가 있습니까?
현재 사용하려고 시도하는 코드를 포함하여 세부 정보를 추가 할 수 있도록 편집 중입니다. 이 코드는 결과 표를 생성하지 않습니다.
추가 편집 : 아래의 Anna가 문제가 UISearchDisplayController에있을 수 있다고 지적했지만 작업 예제 프로젝트에서 현재 코드를 찢어서 실제로 상황이 잘못되거나 왜 UISearchDisplayController 결과가 표시되지 않습니다.
헤더 파일 :
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface CallACabViewController : UIViewController <CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate>
{
CLLocationManager *locationManager;
MKLocalSearch *localSearch;
MKLocalSearchResponse *results;
}
-(IBAction)closeButtonClicked:(id)sender;
@end
구현 파일 :
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[self.searchDisplayController setDelegate:self];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - LocationUpdating
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count > 1) {
oldLocation = [locations objectAtIndex:locations.count-2];
} else {
oldLocation = nil;
}
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.00, 1500.00);
[self performSearch:userLocation];
}
#pragma mark - Search Methods
-(void)performSearch:(MKCoordinateRegion)aRegion
{
// Cancel any previous searches.
[localSearch cancel];
[locationManager stopUpdatingLocation];
// Perform a new search.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"taxi";
request.region = aRegion;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (error != nil) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil)
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
return;
}
if ([response.mapItems count] == 0) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
message:nil
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
return;
}
results = response;
[self.searchDisplayController.searchResultsTableView reloadData];
}];
NSLog(@"DEBUG");
}
#pragma mark - TableView Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [results.mapItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *IDENTIFIER = @"SearchResultsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
}
MKMapItem *item = results.mapItems[indexPath.row];
cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.searchDisplayController setActive:NO animated:YES];
}
-(IBAction)closeButtonClicked:(id)sender
{
[locationManager stopUpdatingLocation];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
MKLocalSearch에는지도가 전혀 필요하지 않습니다. 그냥 문자열을주고 원하는 결과를 사용하십시오. – Anna
경고보기 중 하나가 표시됩니까 ("오류"또는 "결과 없음")? 'response'에 아이템이 있지만 검색 테이블 뷰가 비어 있다면, MIS는 UISearchDisplayController와 MKLocalSearch가 아닌 문제 일 수 있습니다. 다시 말하지만지도는 필요 없지만 요청한 지역에는 실제로 결과가 없을 수 있습니다. – Anna
아니요, UIAlerts를 표시하지 않습니다. 타이머 검색을 수행하면 결과 인스턴스가 0이 아니며 mapItems가 표시되어 UISearchDisplayController에 문제가 있다는 평가에 동의하게됩니다 . – ColinMasters