访问来自 google 个位置 api ios 的结果以自动完成

Access results from google places api ios for auto completion

我正在使用此方法获取建议,我需要在表格视图中显示它们:

- (void)placeAutocomplete:(NSString *)autoCompleteString {
    [self.autoCompleteSuggestionsList removeAllObjects];
    GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
    filter.type = kGMSPlacesAutocompleteTypeFilterCity;

    [_placesClient autocompleteQuery:(NSString *)autoCompleteString
                              bounds:nil
                              filter:filter
                            callback:^(NSArray *results, NSError *error) {
                                if (error != nil) {
                                    NSLog(@"Autocomplete error %@", [error localizedDescription]);
                                    return;
                                }

                                for (GMSAutocompletePrediction* result in results) {
                                    //NSLog(@"Result '%@' with placeID %@", result.attributedFullText.string, result.placeID);
                                    //NSRange autoCompleteRange = [result.attributedFullText.string rangeOfString:autoCompleteString];
                                    //if (autoCompleteRange.location == 0) {
                                    //NSString *stringNow = [NSString stringWithFormat:@"%@",result.attributedFullText.string];
                                    [self.autoCompleteSuggestionsList addObject:result.attributedFullText.string];
                                        //NSLog(@"test : %@",stringNow);
                                    //NSLog(@"%@",self.autoCompleteSuggestionsList);
                                    //}
                                }
                            }];
    [self.autocompleteTableView reloadData];
    NSLog(@"%@",self.autoCompleteSuggestionsList);
}

但我无法访问 autocompleteQuery 方法之外的结果

记录后,它在方法内部正确显示,但在外部不正确, 我正在使用可变数组访问它,但我在内部正确显示,但在外部显示不正确。

我不需要关于使用任何第三方自动完成窗格的建议。 我得到的结果是我只需要从方法中访问它们,这样就可以访问它来显示 tableview

您必须重新加载块内的数据。

Reason for doing this is simple because block is run in different thread so when it complete the execution it come in callback block with main thread thats why we need to reload table in block.

- (void)placeAutocomplete:(NSString *)autoCompleteString {
    [self.autoCompleteSuggestionsList removeAllObjects];
    GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
    filter.type = kGMSPlacesAutocompleteTypeFilterCity;

    [_placesClient autocompleteQuery:(NSString *)autoCompleteString
                              bounds:nil
                              filter:filter
                            callback:^(NSArray *results, NSError *error) {
                                if (error != nil) {
                                    NSLog(@"Autocomplete error %@", [error localizedDescription]);
                                    return;
                                }

                                for (GMSAutocompletePrediction* result in results) {

                                    [self.autoCompleteSuggestionsList addObject:result.attributedFullText.string];

                                }
                                [self.autocompleteTableView reloadData];
                            }];
    NSLog(@"%@",self.autoCompleteSuggestionsList);
}