我使用下面的代码搜索地点并获取我在api(https://maps.googleapis.com/maps/api/place/queryautocomplete/json?input=%@&key=My_Key)下面使用的地点列表。它工作正常,但每次搜索只显示两个位置。但是GMSAutoCompleteViewController为每次搜索显示两个以上的位置。如何为每次搜索获得两个以上的位置,如GMSAutoCompleteVC。
#import "ViewController.h"
#import <GooglePlaces/GooglePlaces.h>
@interface ViewController () <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *searchArray;
NSString *strSearch;
}
@property (weak, nonatomic) IBOutlet UITextField *searchTextfeild;
@property (weak, nonatomic) IBOutlet UITableView *tbl_vw1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_searchTextfeild.delegate = self;
_tbl_vw1.delegate = self;
_tbl_vw1.dataSource = self;
_tbl_vw1.hidden = YES;
_searchTextfeild.clearButtonMode = UITextFieldViewModeAlways;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)LoadJson_search{
searchArray=[[NSMutableArray alloc]init];
// NSLog(@"str......%@",strSearch);
// This API key is from https://developers.google.com/maps/web/
NSString *str1 = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?input=%@&key= My_Key", strSearch];
NSURL *url = [NSURL URLWithString:str1];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error=nil;
if(data.length==0)
{
}
else
{
NSDictionary *jsondic= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
// NSLog(@"1,,,,%@",jsondic);
[searchArray removeAllObjects];
if([[jsondic objectForKey:@"status"]isEqualToString:@"ZERO_RESULTS"])
{
}
else if([[jsondic objectForKey:@"status"]isEqualToString:@"INVALID_REQUEST"])
{
}
else
{
for(int i=0;i<[jsondic.allKeys count];i++)
{
NSString *str1=[[[jsondic objectForKey:@"predictions"] objectAtIndex:i] objectForKey:@"description"];
[searchArray addObject:str1];
}
_tbl_vw1.hidden = NO;
// NSLog(@"%@", searchArray);
}
if (searchArray.count == 0) {
_tbl_vw1.hidden = YES;
}else{
[_tbl_vw1 reloadData];
}
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;{
if (textField.tag == 3) {
strSearch = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([string isEqualToString:@" "]){
}else{
[self LoadJson_search];
}
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
_tbl_vw1.hidden = YES;
[_tbl_vw1 reloadData];
return YES;
}
//TableView delegates
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return searchArray.count;
}
-(UITableViewCell *)tableView:(UITableView*)aTableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
UITableViewCell *cell = [_tbl_vw1 dequeueReusableCellWithIdentifier:@"cell"];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
// NSLog(@"searchArray ==== %@", searchArray);
cell.textLabel.text = [searchArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_searchTextfeild.text = [searchArray objectAtIndex:indexPath.row];
_tbl_vw1.hidden = YES;
[_tbl_vw1 reloadData];
}
答案 0 :(得分:0)
我改变了我的代码......
我得到的地方超过两个,但是我使用了json count [jsondic.allKeys count]
for(int i=0;i<[jsondic.allKeys count];i++)
{
NSString *str1=[[[jsondic objectForKey:@"predictions"] objectAtIndex:i] objectForKey:@"description"];
[searchArray addObject:str1];
}
现在我只有两个地方
现在我将json计数从[jsondic.allKeys count]更改为[[jsondic objectForKey:@“predictions”] count]
for(int i=0;i<[[jsondic objectForKey:@"predictions"] count];i++)
{
NSString *str1=[[[jsondic objectForKey:@"predictions"] objectAtIndex:i] objectForKey:@"description"];
[searchArray addObject:str1];
}
现在我有两个以上的地方......