您必须根据需要使用 google 地方搜索 API。
首先,您必须从谷歌开发者控制台获取 API 密钥。
并使用以下 URL 获取搜索位置
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=YOUR_API_KEY
从Github下载库
步骤 1)首先在 viewController.h 中的 @interface class 之前添加类 @class SPGooglePlacesAutocompleteQuery;>>>>>>
#import <UIKit/UIKit.h>
@class SPGooglePlacesAutocompleteQuery;
@interface ViewController : UIViewController
{
SPGooglePlacesAutocompleteQuery *searchQuery;
}
@property (nonatomic,strong) NSMutableArray *mutArrSearchData;
@end
然后你的 viewDidLoad >>>
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mutArrSearchData = [NSMutableArray array];
searchQuery = [[SPGooglePlacesAutocompleteQuery alloc] init];
self.mutArrSearchData = [NSMutableArray array];
}
步骤 2)然后如果您使用 UISearchBar 然后设置委托并添加委托方法。对于 EX:
- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText{
if (searchBar.text.length>=1) {
[self handleSearchForSearchString:sender.text];
}
}
- (void)handleSearchForSearchString:(NSString *)searchString {
[self.mutArrSearchData removeAllObjects];
searchQuery.location = self.mapView.userLocation.coordinate;
searchQuery.input = searchString;
[searchQuery fetchPlaces:^(NSArray *places, NSError *error) {
if (error) {
SPPresentAlertViewWithErrorAndTitle(error, @"Could not fetch Places");
} else {
// you have got your related responce in this array
[self.mutArrSearchData addObjectsFromArray:places];
NSLog(@"responce:%@",self.mutArrSearchData);
//[self.tableview reloadData];
}
}];
}
你已经在 up 方法中得到了响应。现在你可以在你的表格视图中处理这个响应。和 didSelect 方法....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
yourCell *aCell;
[aCell.yourLbl.text setText:[self placeAtIndexPath:indexPath].name];
return aCell;
}
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SPGooglePlacesAutocompletePlace *place = [self placeAtIndexPath:indexPath];
[place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {
if (error) {
SPPresentAlertViewWithErrorAndTitle(error, @"Could not map selected Place");
} else if (placemark) {
// your selected placemark here. get data from placemark which you needed.
}
}];
}
- (SPGooglePlacesAutocompletePlace *)placeAtIndexPath:(NSIndexPath *)indexPath {
return [self.mutArrSearchData objectAtIndex:indexPath.row];
}
确保从谷歌开发者控制台获取 API_KEY。
希望对你有帮助。