Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply custom search on uitextfield for place search using gmsAutocomplete view controller.?

HI all i am working on GMSAutocompleteViewController in my app. i have a uitextfield in my view controller when i tapp textfield for search a place using google api, a new view is open which is powered by google. Look at my code please.

- (void)textFieldDidBeginEditing:(UITextField *)textField
 {



tappedTextField = textField;
        GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
        acController.delegate = self;
        [self presentViewController:acController animated:YES completion:nil];



  }


  - (void)viewController:(GMSAutocompleteViewController *)viewController
   didAutocompleteWithPlace:(GMSPlace *)place {
// Do something with the selected place.
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
NSLog(@"lat and log%f", place.coordinate.latitude);
NSLog(@"lang %f", place.coordinate.longitude);



   tappedTextField.text = place.name;



[self dismissViewControllerAnimated:YES completion:nil];
}

  - (void)viewController:(GMSAutocompleteViewController *)viewController
    didFailAutocompleteWithError:(NSError *)error {
// TODO: handle the error.
      NSLog(@"error: %ld", (long)[error code]);
     [self dismissViewControllerAnimated:YES completion:nil];
     }

  // User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
NSLog(@"Autocomplete was cancelled.");
[self dismissViewControllerAnimated:YES completion:nil];
   }

i don't want to move to another view on textfield tapp for searching. is it possible that i can search places from the textfield only?

Scrren of my view controller

when i click on destination textfield a new view is appear for searching but i need search from textfield only please at the screen after tapp the textfield.

This screen after click of textfield

like image 852
sandeep tomar Avatar asked May 03 '16 09:05

sandeep tomar


2 Answers

It sounds like what you're after is to display the autocomplete results in a table immediately underneath the textfield when the text field is active (ie something similar to this).

You can do this by creating a UITableView that you display in your UI in the right location. Instead of using GMSAutocompleteViewController, create a GMSAutocompleteTableDataSource and set it as the UITableView's delegate and data source. For example:

_tableDataSource = [[GMSAutocompleteTableDataSource alloc] init];
_tableDataSource.delegate = self;
tableView.delegate = _tableDataSource;
tableView.dataSource = _tableDataSource;

When the text field text changes, call sourceTextHasChanged on the table data source.

[_tableDataSource sourceTextHasChanged:textField.text];

Finally, have the parent view controller implement the GMSAutocompleteTableDataSourceDelegate protocol

There is some example code in the sample app contained in the GoogleMaps CocoaPod that is close to what you want. Look for SDKDemoAutocompleteWithTextFieldController.m in the downloaded Pods directory.

like image 78
AndrewR Avatar answered Oct 23 '22 06:10

AndrewR


I used this code to achieve same behaviour you are asking.

-(void)LoadJson_search{
searchArray=[[NSMutableArray alloc]init];
NSLog(@"str......%@",strSearch);

NSString *str1 = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&key=AIzaSyD2NttUhPQ4PKvpju97qpeWj8SYnZtzt0s",strSearch];
NSLog(@"%@",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++)
        {
            //['predictions'][0]['description']
            NSString *str1=[[[jsondic objectForKey:@"predictions"] objectAtIndex:i] objectForKey:@"description"];
            [searchArray  addObject:str1];
        }
        tbl_vw1.hidden=FALSE;
    }
    if (searchArray.count == 0) {
        tbl_vw1.hidden = TRUE;
    }
    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;
}

And on tableview didSelect method use following

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
strSelectedAddress = [searchArray objectAtIndex:indexPath.row];
[self.eventDict setObject:strSelectedAddress forKey:@"venue"];
//[self geoCodeUsingAddress:str_select_address];

tbl_vw1.hidden=TRUE;
[tbl_vw reloadData];
}

after reloading table, use this in cellForRowAtIndexpath to update selected place name.

cell.txt.text = [self.eventDict objectForKey:@"venue"];

Hope this helps you. :)

like image 1
Nij Avatar answered Oct 23 '22 07:10

Nij