我已经使用Google地方信息实现了自动完成功能,并且效果很好(当用户在文本字段中输入位置时,会在表格视图中显示自动完成结果)。也就是说,我是否有可能从返回/选择的结果中仅捕获城市或省份?我需要将此值存储在数据库中。
以下代码:
- (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=AIzaSyAm7buitimhMgE1dKV2j4_7doULluiiDzU", 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];
}
self.tableView.hidden = NO;
// NSLog(@"%@", searchArray);
}
if (searchArray.count == 0) {
self.tableView.hidden = YES;
}else{
[self.tableView reloadData];
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.addressField.text = [searchArray objectAtIndex:indexPath.row];
self.tableView.hidden = YES;
[self.tableView reloadData];
}