#import "AppDelegate.h"
#import <GoogleMaps/GoogleMaps.h>
@interface AppDelegate ()
@end
@import GooglePlaces;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[GMSPlacesClient provideAPIKey:@"Your key here"];
return YES;
}
#import "ViewController.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;
//Resize tableview when display keyboard (show keyboard and hide keyboard)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//When keyboard show
-(void)keyboardShown:(NSNotification *)note {
CGRect keyboardFrame;
[[[note userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
CGRect tableViewFrame = _tbl_vw1.frame;
tableViewFrame.size.height -= keyboardFrame.size.height;
[_tbl_vw1 setFrame:tableViewFrame];
}
//When keyboard hide
-(void)keyboardHidden:(NSNotification *)note {
CGFloat maxY = CGRectGetMaxY(_searchTextfeild.frame);
CGFloat height = CGRectGetHeight(_searchTextfeild.frame);
height = height+20;
[_tbl_vw1 setFrame:CGRectMake(0, maxY, self.view.frame.size.width, self.view.frame.size.height-height)];
}
- (void)LoadJson_search{
searchArray=[[NSMutableArray alloc]init];
NSString *str1 = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=geocode&language=fr&key=your key here", 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{
dispatch_async(dispatch_get_main_queue(), ^{
[_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;
dispatch_async(dispatch_get_main_queue(), ^{
[_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;
dispatch_async(dispatch_get_main_queue(), ^{
[_tbl_vw1 reloadData];
});
}
No comments:
Post a Comment