我是iOS新手。我正在使用苹果公司的地图。我需要一些功能,比如goole PlaceAutoComplete。
在此之前我尝试使用PlaceAutoComplete通过pod添加googlePlaces实用程序,并且函数回调甚至没有响应。我已经使用googleMap placeAutoComplete实用程序创建了一个iOS Api Key。
这是我的一段代码
-(void)placeAutocomplete{
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterCity;
[self.placesClients autocompleteQuery:self.txtFld.text
bounds:nil
filter:filter
callback:^(NSArray *results,NSError*error)
{
if (error != nil) {
NSLog(@"Autocomplete error %@", [error localizedDescription]);
return;
}
self.autoCompleteArray = results;
}];
}
答案 0 :(得分:0)
您可以使用搜索栏或制作文本字段的事件textDidChange
来完成此操作我使用下面的代码完成了
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(requestForGoogleAutoCompletWithTask) object:nil];
[self performSelector:@selector(requestForGoogleAutoCompletWithTask) withObject:nil afterDelay:0.0];
}
-(void)requestForGoogleAutoCompletWithTask
{
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc]init];
[filter setType:kGMSPlacesAutocompleteTypeFilterNoFilter];
GMSPlacesClient *placesClient = [GMSPlacesClient sharedClient];
[placesClient autocompleteQuery:sBar.text
bounds:nil
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(@"Autocomplete error %@", [error localizedDescription]);
arrAutoCompletData = results; // This is my actual array which I am showing in UITableView
[tblView reloadData];
return;
}
arrAutoCompletData = results;
[tblView reloadData];
for (GMSAutocompletePrediction* result in results)
{
NSLog(@"Result '%@' with placeID %@", result.attributedFullText.string, result.placeID);
}
}];
}