2016-05-30 3 views
1

Я пробовал читать API google places. и попытался дублировать их работу. Но я думаю, что здесь не хватает некоторых шагов.Результат поиска auto complete не отображается iOS Objective C

Вот код для заголовка моего заголовочного файла.

@class SPGooglePlacesAutocompleteQuery; 

@interface GoogleMapViewViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate, MKMapViewDelegate, UISearchControllerDelegate> 
{ 
    NSArray *searchResultPlaces; 
    SPGooglePlacesAutocompleteQuery *searchQuery; 
    MKPointAnnotation *selectedPlaceAnnotation; 

    BOOL shouldBeginEditing; 
} 

@property (strong, nonatomic) UISearchController *searchController; 
@property (retain, nonatomic) IBOutlet MKMapView *mapView; 

@end 

Мой файл реализации

#import "GoogleMapViewViewController.h" 
#import "SPGooglePlacesAutocompleteQuery.h" 
#import "SPGooglePlacesAutocompletePlace.h" 


@interface GoogleMapViewViewController() 
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; 

@end 

@implementation GoogleMapViewViewController 
@synthesize mapView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     searchQuery = [[SPGooglePlacesAutocompleteQuery alloc] init]; 
     searchQuery.radius = 100.0; 
     shouldBeginEditing = YES; 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.searchBar.placeholder = @"Search or Address"; 
    self.searchBar.delegate = self; 
} 

- (void)viewDidUnload { 
    [self setMapView:nil]; 
    [super viewDidUnload]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 



#pragma mark - 
#pragma mark UITableViewDataSource 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [searchResultPlaces count]; 
} 

- (SPGooglePlacesAutocompletePlace *)placeAtIndexPath:(NSIndexPath *)indexPath { 
    return [searchResultPlaces objectAtIndex:indexPath.row]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"SPGooglePlacesAutocompleteCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    cell.textLabel.font = [UIFont fontWithName:@"GillSans" size:16.0]; 
    cell.textLabel.text = [self placeAtIndexPath:indexPath].name; 
    return cell; 
} 

#pragma mark UITableViewDelegate 

- (void)recenterMapToPlacemark:(CLPlacemark *)placemark { 
    MKCoordinateRegion region; 
    MKCoordinateSpan span; 

    span.latitudeDelta = 0.02; 
    span.longitudeDelta = 0.02; 

    region.span = span; 
    region.center = placemark.location.coordinate; 

    [self.mapView setRegion:region]; 
} 

- (void)addPlacemarkAnnotationToMap:(CLPlacemark *)placemark addressString:(NSString *)address { 
    [self.mapView removeAnnotation:selectedPlaceAnnotation]; 

    selectedPlaceAnnotation = [[MKPointAnnotation alloc] init]; 
    selectedPlaceAnnotation.coordinate = placemark.location.coordinate; 
    selectedPlaceAnnotation.title = address; 
    [self.mapView addAnnotation:selectedPlaceAnnotation]; 
} 

- (void)dismissSearchControllerWhileStayingActive { 
    // Animate out the table view. 
    NSTimeInterval animationDuration = 0.3; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:animationDuration]; 
    self.searchDisplayController.searchResultsTableView.alpha = 0.0; 
    [UIView commitAnimations]; 

    [self.searchDisplayController.searchBar setShowsCancelButton:NO animated:YES]; 
    [self.searchDisplayController.searchBar resignFirstResponder]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    SPGooglePlacesAutocompletePlace *place = [self placeAtIndexPath:indexPath]; 
    [place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error) { 
     if (error) { 
      SPPresentAlertViewWithErrorAndTitle(error, @"Could not map selected Place"); 
     } else if (placemark) { 
      [self addPlacemarkAnnotationToMap:placemark addressString:addressString]; 
      [self recenterMapToPlacemark:placemark]; 
      [self dismissSearchControllerWhileStayingActive]; 
      [self.searchDisplayController.searchResultsTableView deselectRowAtIndexPath:indexPath animated:NO]; 
     } 
    }]; 
} 

#pragma mark UISearchDisplayDelegate 

- (void)handleSearchForSearchString:(NSString *)searchString { 
    searchQuery.location = self.mapView.userLocation.coordinate; 
    searchQuery.input = searchString; 
    [searchQuery fetchPlaces:^(NSArray *places, NSError *error) { 
     if (error) { 
      SPPresentAlertViewWithErrorAndTitle(error, @"Could not fetch Places"); 
     } else { 
      searchResultPlaces = places; 
      [self.searchDisplayController.searchResultsTableView reloadData]; 
     } 
    }]; 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self handleSearchForSearchString:searchString]; 

    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 


- (BOOL)searchController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self handleSearchForSearchString:searchString]; 

    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 



#pragma mark UISearchBar Delegate 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    if (![searchBar isFirstResponder]) { 
     // User tapped the 'clear' button. 
     shouldBeginEditing = NO; 
     [self.searchDisplayController setActive:NO]; 
     [self.mapView removeAnnotation:selectedPlaceAnnotation]; 
    } 
} 

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { 
    if (shouldBeginEditing) { 
     // Animate in the table view. 
     NSTimeInterval animationDuration = 0.3; 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:animationDuration]; 
     self.searchDisplayController.searchResultsTableView.alpha = 1.0; 
     [UIView commitAnimations]; 

     [self.searchDisplayController.searchBar setShowsCancelButton:YES animated:YES]; 
    } 
    BOOL boolToReturn = shouldBeginEditing; 
    shouldBeginEditing = YES; 
    return boolToReturn; 
} 

#pragma mark MKMapView Delegate 

- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id <MKAnnotation>)annotation { 
    if (mapViewIn != self.mapView || [annotation isKindOfClass:[MKUserLocation class]]) { 
     return nil; 
    } 
    static NSString *annotationIdentifier = @"SPGooglePlacesAutocompleteAnnotation"; 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 
    if (!annotationView) { 
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; 
    } 
    annotationView.animatesDrop = YES; 
    annotationView.canShowCallout = YES; 

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [detailButton addTarget:self action:@selector(annotationDetailButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    annotationView.rightCalloutAccessoryView = detailButton; 

    return annotationView; 
} 

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    // Whenever we've dropped a pin on the map, immediately select it to present its callout bubble. 
    [self.mapView selectAnnotation:selectedPlaceAnnotation animated:YES]; 
} 

- (void)annotationDetailButtonPressed:(id)sender { 
    // Detail view controller application logic here. 
} 


@end 

Я действительно путают теперь мой файл реализации, как я не могу понять, что там TBH.plus некоторые коды здесь в основном устарели. Кто-то хочет дать подробное руководство по этому поводу? или объясните мне в срок непрофессионала. ТИА.

+0

вы можете показать свою раскадровку? Я думаю, что выходы перепутаны, а делегаты настроены неправильно. – satheeshwaran

+0

Я не могу отправить свой скриншот напрямую, но у меня есть только панель поиска и MKMapView в моем файле xib ... – EdBer

+0

Приложите скриншот, показывающий все сопоставления. Используете ли вы UISearchDisplayController или тот, который встроен в UITableViewController? или просто простой UISearchBar? – satheeshwaran

ответ

0

ОТВЕТИТ!

В основном моя проблема была в этой функции, заданной на образец проекте API Google Адреса ..

BOOL SPEnsureGoogleAPIKey() { 
    BOOL userHasProvidedAPIKey = YES; 
    if (![kGoogleAPIKey isEqualToString:@"AIzaSyA2vs9pJoLrLs6XU8IRVHo7WxiuMufYXl8"]) { 
     userHasProvidedAPIKey = NO; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"API Key Needed" message:@"Please replace kGoogleAPIKey with your Google API key." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
     [alert show]; 
    } 
    return userHasProvidedAPIKey; 
} 

если заявление изначально неверно, поэтому он всегда дает мне неправильное значение. теперь его работа :) просто добавил не "!" если

+0

Grrrr !!!! эта ошибка должна была сказать вам, что это проблема, так или иначе вы обнаружили проблему. – satheeshwaran

 Смежные вопросы

  • Нет связанных вопросов^_^