I used below code to search places and to get places list I used below api (https://maps.googleapis.com/maps/api/place/queryautocomplete/json?input=%@&key=My_Key). It's working fine, but it's showing only two places in each search. But the GMSAutoCompleteViewController shows more than two places for each search. How to get more than two places for each search like 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];
}
I changed in my code like this...
I'm getting places more than two, but i used 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];
}
Now i'm getting only two places
Now i changed json count from [jsondic.allKeys count] to [[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];
}
Now i'm getting more than two places.....