san*_*mar 5 google-maps autocomplete ios
我正在我的应用程序中使用GMSAutocompleteViewController.我在视图控制器中有一个uitextfield当我使用谷歌api搜索文本字段搜索一个地方时,一个新视图打开,由谷歌提供支持.请看我的代码.
- (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];
}
Run Code Online (Sandbox Code Playgroud)
我不想移动到textfield tapp上的另一个视图进行搜索.我可以只从文本字段中搜索地点吗?
当我点击目标文本字段时,会出现一个用于搜索的新视图,但我只需要在文本字段之后在屏幕上搜索文本字段.
听起来您想要的是当文本字段处于活动状态时,在文本字段正下方的表格中显示自动完成结果(即与此类似的内容)。
您可以通过创建一个显示在 UI 中正确位置的 UITableView 来实现此目的。不使用GMSAutocompleteViewController,而是创建一个GMSAutocompleteTableDataSource并将其设置为 UITableView 的委托和数据源。例如:
_tableDataSource = [[GMSAutocompleteTableDataSource alloc] init];
_tableDataSource.delegate = self;
tableView.delegate = _tableDataSource;
tableView.dataSource = _tableDataSource;
Run Code Online (Sandbox Code Playgroud)
当文本字段文本发生变化时,调用sourceTextHasChanged表数据源。
[_tableDataSource sourceTextHasChanged:textField.text];
Run Code Online (Sandbox Code Playgroud)
最后,让父视图控制器实现该GMSAutocompleteTableDataSourceDelegate协议
GoogleMaps CocoaPod 中包含的示例应用程序中有一些示例代码,与您想要的很接近。SDKDemoAutocompleteWithTextFieldController.m在下载的 Pods 目录中查找。
| 归档时间: |
|
| 查看次数: |
4074 次 |
| 最近记录: |