Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
搜索结果自动完成不显示iOS目标C_Ios_Objective C_Mapkit_Uisearchbar_Uisearchcontroller - Fatal编程技术网

搜索结果自动完成不显示iOS目标C

搜索结果自动完成不显示iOS目标C,ios,objective-c,mapkit,uisearchbar,uisearchcontroller,Ios,Objective C,Mapkit,Uisearchbar,Uisearchcontroller,我试过阅读GooglePlacesAPI。并试图复制他们的作品。但我想我错过了一些步骤 这是我的头文件的代码 @class SPGooglePlacesAutocompleteQuery; @interface GoogleMapViewViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate, MK

我试过阅读GooglePlacesAPI。并试图复制他们的作品。但我想我错过了一些步骤

这是我的头文件的代码

@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
@class SPGooglePlacesAutocompleteQuery;
@界面GoogleMapViewController:UIViewController
{
NSArray*搜索结果位置;
SPGooglePlacesAutocompleteQuery*搜索查询;
MKPointAnnotation*选择地点annotation;
布尔应该开始编辑;
}
@属性(强,非原子)UISearchController*searchController;
@属性(保留,非原子)IBMOutlet MKMapView*mapView;
@结束
我的实现文件

#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
#导入“GoogleMapViewController.h”
#导入“SPGooglePlacesAutocompleteQuery.h”
#导入“SPGooglePlacesAutocompletePlace.h”
@界面GoogleMapViewController()
@属性(弱、非原子)IBUISearchBar*搜索栏;
@结束
@GoogleMapViewController的实现
@综合地图视图;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
searchQuery=[[SPGooglePlacesAutocompleteQuery alloc]init];
searchQuery.radius=100.0;
shouldBeginEditing=是;
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
self.searchBar.placeholder=@“搜索或地址”;
self.searchBar.delegate=self;
}
-(无效)视图卸载{
[自设置映射视图:无];
[超级视频下载];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
/*
#pragma标记-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方{
//使用[segue destinationViewController]获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
#布拉格标记-
#pragma标记UITableViewDataSource
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[searchResultPlaces计数];
}
-(SPGooglePlacesAutocompletePlace*)placeAtIndexPath:(NSIndexPath*)indexPath{
返回[searchResultPlaces objectAtIndex:indexath.row];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*cellIdentifier=@“SPGooglePlacesAutocompleteCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier];
}
cell.textlab.font=[UIFont fontWithName:@“GillSans”大小:16.0];
cell.textlab.text=[self-placeAtIndexPath:indexath].name;
返回单元;
}
#pragma标记UITableViewDelegate
-(无效)重新居中映射位置标记:(CLPlacemark*)位置标记{
协调区域;
Mk坐标跨度;
span.latitudeDelta=0.02;
span.longitudeDelta=0.02;
region.span=span;
region.center=placemark.location.coordinate;
[self.mapView setRegion:region];
}
-(无效)AddPlaceMarkAnnotationMap:(CLPlacemark*)PlaceMarkAddressString:(NSString*)地址{
[self.mapView removeAnnotation:selectedPlaceAnnotation];
selectedPlaceAnnotation=[[MKPointAnnotation alloc]init];
selectedPlaceAnnotation.coordinate=placemark.location.coordinate;
selectedPlaceAnnotation.title=地址;
[self.mapView addAnnotation:selectedPlaceAnnotation];
}
-(无效)解除SearchControllerHirestaingActive{
//设置表格视图的动画。
NSTimeInterval animationDuration=0.3;
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDuration:animationDuration];
self.searchDisplayController.searchResultsTableView.alpha=0.0;
[UIView委员会];
[self.searchDisplayController.searchBar设置显示扫描按钮:否动画:是];
[self.searchDisplayController.searchBar辞职FirstResponder];
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
SPGooglePlacesAutocompletePlace*place=[self-placeAtIndexPath:indexPath];
[place resolveToPlacemark:^(CLPlacemark*placemark,NSString*addressString,NSError*错误){
如果(错误){
SPPresentAlertViewWithErrorAndTitle(错误,@“无法映射所选位置”);
}else if(位置标记){
[self-AddPlaceMarkAnnotationMap:PlaceMarkAddressString:addressString];
[自重新居中映射位置标记:位置标记];
[自解职搜索控制器处于活动状态];
[self.searchDisplayController.searchResultsTableView取消行索引路径:indexPath动画:否];
}
}];
}
#pragma标记UISearchDisplayDelegate
-(void)handleSearchForSearchString:(NSString*)搜索字符串{
searchQuery.location=self.mapView.userLocation.coordinate;
searchQuery.input=searchString;
[searchQuery fetchPlaces:^(NSArray*places,NSError*error){
如果(错误){
SPPresentAlertViewWithErrorAndTitle(错误,@“无法获取位置”);
}否则{
searchResultPlaces=地点;
[self.searchDisplayController.searchResultsTableView重新加载数据];
}
}];
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)控制器应重新加载搜索字符串的表:(NSString*)搜索字符串{
[self-handleSearchForSearchString:searchString];
//返回YES以重新加载搜索结果表视图。
返回YES;
}
-(BOOL)searchController:(UISearchController*)控制器应为searchString:(NSString*)searchString重新加载表{
[self-handleSearchForSearchString:searchString];
//返回YES以重新加载搜索结果表视图。
返回YES;
}
#pragma标记UISearchBar委托
-(无效)搜索栏:(UISearchBar*)搜索栏文本
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;
}