,你必須使用谷歌的地方搜索API您的需要。 首先,您必須從Google開發者控制檯獲取API密鑰。 及以下網址使用的GET搜尋的地點 https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=YOUR_API_KEY
下載庫從Github
第1步)第一添加類@class SPGooglePlacesAutocompleteQuery;
在viewController.h的@interface class
前>>>>>>
#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];
}
}];
}
你已經得到了迴應方法。現在你可以在你的tableview中提供這個響應。和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。
希望它可以幫助你。
來源
2016-02-20 13:18:38
Vvk
也從谷歌自動完成API https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=YOUR_API_KEY –
我不知道這個過程得到的地址。請告訴我如何做這個過程? – vijetha
我正在使用蘋果地圖,但爲什麼我們要使用谷歌API? – vijetha