2016-05-30 47 views
1

我试过阅读谷歌地方的API。并试图重复他们的工作。但我想我在这里错过了一些步骤。搜索结果自动完成不显示iOS目标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这里的一些代码大多已过时。有人关心这方面的详细指导?或以外行人的名义向我解释。 TIA。

+0

你可以显示你的故事板吗?我猜想网点已经搞砸了,代表没有正确设置。 – satheeshwaran

+0

我无法直接发送截图,但我只有一个搜索栏和一个MKMapView在我的xib文件中... – EdBer

+0

附上显示所有映射的截图。您是使用UISearchDisplayController还是嵌入了UITableViewController的?或者只是一个普通的UISearchBar? – satheeshwaran

回答

0

答案!

基本上我的问题是关于谷歌的样本项目给这个函数将API ..

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

if语句原本是不正确,这就是为什么它总是给我错误的值。现在它的工作:)只是添加了“!” in if

+0

Grrrr !!!!那个错误变量应该告诉你这是问题,反正你发现了这个问题。 – satheeshwaran