2017-06-07 2 views
0

J'ai fait ce code autocomplete pour la recherche des lieux. Et recherche correctement mais les limites et la région ne sont pas définies. montrer des lieux hors de mes limites.Bounds ne fonctionne pas pour GMSAutocompleteFetcher dans l'API Google place dans iOS Objective C

#import "SearchViewController.h" 
#import <GooglePlaces/GooglePlaces.h> 
#import <GoogleMaps/GoogleMaps.h> 
@import GooglePlaces; 


@interface SearchViewController() <GMSAutocompleteViewControllerDelegate,GMSAutocompleteFetcherDelegate> 
@end 
@implementation SearchViewController{ 
    UITextField *_textField; 
    UITextView *_resultText; 
    GMSAutocompleteFetcher* _fetcher; 
    GMSPlacesClient *_placesClient; 
    GMSCoordinateBounds *_bounds; 
    GMSAutocompleteFilter *_filter; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //APIkEy 
    [GMSPlacesClient provideAPIKey:@"AIzaSyBYA7KlOZ3VqjQTG4x0R1jaCt9I1-1EVBc"]; 
_placesClient = [GMSPlacesClient sharedClient]; 
self.view.backgroundColor = [UIColor whiteColor]; 
    self.edgesForExtendedLayout = UIRectEdgeNone; 
// Set bounds 
    CLLocationCoordinate2D neBoundsCorner = CLLocationCoordinate2DMake(35.818074, 51.586990); 
    CLLocationCoordinate2D swBoundsCorner = CLLocationCoordinate2DMake(35.557071, 51.098099); 
    _bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:neBoundsCorner coordinate:swBoundsCorner]; 
// Set up the autocomplete filter. 
    _filter = [[GMSAutocompleteFilter alloc] init]; 
    _filter.type = kGMSPlacesAutocompleteTypeFilterNoFilter; 
// Create the fetcher. 
    _fetcher = [[GMSAutocompleteFetcher alloc] initWithBounds:_bounds filter:_filter]; 

    _fetcher.delegate = self; 
// Set up the UITextField and UITextView. 
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(5.0f, 0, self.view.bounds.size.width - 5.0f, 44.0f)]; 
    _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 

    _resultText =[[UITextView alloc] initWithFrame:CGRectMake(0, 45.0f, self.view.bounds.size.width, self.view.bounds.size.height - 1.0f)]; 
    _resultText.backgroundColor = [UIColor colorWithWhite:0.95f alpha:1.0f]; 
    _resultText.text = @"No Results"; 
    _resultText.editable = NO; 

    [self.view addSubview:_textField]; 
    [self.view addSubview:_resultText]; 
} 

- (void)textFieldDidChange:(UITextField *)textField { 
    NSLog(@"%@", textField.text); 
    [_fetcher sourceTextHasChanged:textField.text]; 
} 
#pragma mark - GMSAutocompleteFetcherDelegate 
- (void)didAutocompleteWithPredictions:(NSArray *)predictions { 
    NSMutableString *resultsStr = [NSMutableString string]; 



    for (GMSAutocompletePrediction *prediction in predictions) { 
     [resultsStr appendFormat:@"%@\n", [prediction.attributedPrimaryText string]]; 
     NSLog(@"sec is: %@",[prediction.attributedSecondaryText string]); 
     NSLog(@"placeID is: %@", prediction.placeID); 


     [_placesClient lookUpPlaceID:prediction.placeID callback:^(GMSPlace *place, NSError *error) { 
      if (error != nil) { 
       NSLog(@"Place Details error %@", [error localizedDescription]); 
       return; 
      } 

      if (place != nil) { 
       NSLog(@"Place name %@", place.name); 
       NSLog(@"Place address %@", place.formattedAddress); 
       NSLog(@"Place placeID %@", place.placeID); 
       NSLog(@"Place attributions %@", place.attributions); 
       NSLog(@"Place latitude %f", place.coordinate.latitude); 
       NSLog(@"Place longitude %f", place.coordinate.longitude); 
      } else { 
       NSLog(@"No place details for %@", prediction.placeID); 
      } 
     }]; 





    } 
_resultText.text = resultsStr; 
} 

Répondre